2017-01-11 16 views
2

私のアプリは緯度と経度を取得できるように(家から仕事に)人の軌跡を取得する必要があります(2つのボタンがあります。 2.停止して緯度と経度を取得します)。しかし、私はアンドロイドがアプリを殺さないようにしたい、ユーザーがFacebookやtwitter(例えば)を使用している間、または単にユーザーがスマートフォンをロックしている間、アプリケーションを実行しておきたい。 スマートフォンをロックすると、FacebookやTwitterなどでアプリを使用するときにアプリが正常に動作しますが、Androidはアプリを強制終了します。 intentserviceとサービスを使用しようとしましたが、画面をロックすると機能しません。 PowerManager.WakeLockを使うべきですか?私はそれがどのように機能し、何をしているのか正確には分かりません。いつもアプリをAndroidでバックグラウンドで実行し続ける

これは私が間違っているかどうかわからないサービスを試すための例ですが、次の場合には機能しません。 1.私はサービスに入っています(サービスを開始しました) 2。私はホームボタンをタップします(サービスの実行中) 3.私は画面をロックします。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.javosoft.intentservice.MainActivity"> 

    <Button 
     android:text="start Service" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="63dp" 
     android:id="@+id/button" 
     android:onClick="startService"/> 

    <Button 
     android:text="Stop Service" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignLeft="@+id/button" 
     android:layout_alignStart="@+id/button" 
     android:layout_marginBottom="68dp" 
     android:id="@+id/button2" 
     android:onClick="stopService" /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="textPersonName" 
     android:hint="textito :D" 
     android:ems="10" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" 
     android:id="@+id/editText" /> 
</RelativeLayout> 

Layout

activity_main 4.次にサービスやアプリがアンドロイドによって殺され(およびサービスは、そのようなものをやって終了しませんでした)

マニフェスト

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.javosoft.intentservice"> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <service android:name=".MyService" 
      android:exported="false"></service> 

    </application> 

</manifest> 

MainActivity

package com.javosoft.intentservice; 

import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 

public class MainActivity extends AppCompatActivity { 

    private Button button; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     button = (Button) findViewById(R.id.button); 

     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Intent intent = new Intent(MainActivity.this, MyService.class); 
       startService(intent); 
      } 
     }); 

    } 

    public void stopService (View view){ 
     Intent intent = new Intent(this, MyService.class); 
     stopService(intent); 
    } 
} 

たMyServiceクラス

package com.javosoft.intentservice; 

import android.app.Service; 
import android.content.Intent; 
import android.os.IBinder; 
import android.support.annotation.Nullable; 
import android.util.Log; 
import android.widget.Toast; 

public class MyService extends Service { 

    final class MyThreadClass implements Runnable{ 

     int service_id; 
     MyThreadClass(int service_id){ 
      this.service_id = service_id; 
     } 

     @Override 
     public void run() { 

      synchronized (this){ 
       int count = 0; 
       while (count < 10){ 
        Log.i("servicio", "while: " + String.valueOf(count)); 
        try { 
         wait(2000); 
         count++; 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 
       } 
      } 
      stopSelf(service_id); 

     } 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     Toast.makeText(this, "El servició ha empezado D:", Toast.LENGTH_SHORT).show(); 
     Log.i("servicio", "El servició ha empezado D:"); 


     Log.i("servicio", "onStartCommand arriba"); 

     Thread thread = new Thread(new MyThreadClass(startId)); 
     thread.start(); 

     Log.i("servicio", "onStartCommand abajo"); 
     return START_STICKY; 
     //return super.onStartCommand(intent, flags, startId); 
    } 

    @Override 
    public void onDestroy() { 
     //super.onDestroy(); 
     Toast.makeText(this, "El servicio ha padarado ._.", Toast.LENGTH_SHORT).show(); 
     Log.i("servicio", "El servicio ha padarado ._."); 
    } 

    //@Nullable 
    @Override 
    public IBinder onBind(Intent intent) { 

     return null; 

    } 
} 

おかげ

+0

ええと、画面を閉じるとサービスが機能するはずです。 – Gudin

+0

私はアプリに入っていて、画面をロックしていますが、アプリにいれば、ホームボタンをタップして画面をロックすると、Androidがアプリを殺します。 –

+0

コードを投稿できますか?サービスを試してみたが、サービスの開始方法と終了方法は重要です。サービスのライフサイクルの方法だけでなく、サービスの開始と終了のコードを投稿するとよいでしょう。私はあなたがサービスでやろうとしていることをやっているので、それを行うことができます。 – Gary99

答えて

0

@Gudinが言うように、あなたが20秒後にサービスを停止するように、それは私には見えます。しかし、これは時々動作すると言われているので、それはちょうどいくつかのテストコードだと思います。私はあなたの問題があなたのサービスの開始方法にあると考えています。アクティビティのコンテキストに合格すると、サービスはそのアクティビティのライフサイクルに関連付けられます。それが破壊された場合、あなたのサービスはそれと一緒に行くと思います。 (単にonStop()のアクティビティ&ではなく、onDestroy()はサービスをそのまま残します)。お試しください

Intent intent = new Intent(getApplicationContext(), MyService.class); 

私はあなたの問題を複製しようとしましたが、私はあなたの問題であることを確認することはできませんでした。これは良いスタート地点です。

1

は、[スタート]ボタンをクリックしたときに、私たちのこの

public class ForegroundService extends Service { 
    private static final String LOG_TAG = "ForegroundService"; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 

     // Your logical code here 

     return START_STICKY; 
    } 

    @Override 
    public void onTaskRemoved(Intent rootIntent) { 

     //When remove app from background then start it again 
     startService(new Intent(this, ForegroundService.class)); 

     super.onTaskRemoved(rootIntent); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     Log.i(LOG_TAG, "In onDestroy"); 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     // Used only in case of bound services. 
     return null; 
    } 
} 

をお試しください:マニフェスト

<service 
      android:name=".ForegroundService" 
      android:enabled="true" 
      android:stopWithTask="false" /> 
0

Intent startIntent = new Intent(MainActivity.this, ForegroundService.class); 
    startService(startIntent); 

私はいくつかのスマートフォンは、問題のこれらの種類を持っていることを読んで、私が見つかりました。そのHuawei、Xiaomi他の中で実装サービスに問題があります私のスマートフォンはHuaweiです。この問題は、省エネのためにこれらのデバイスにプリインストールされたプログラムから発生します。したがって、Nexus(このシミュレータ用のシミュレータ)、ソニー、Moto Gでプログラムを実行しようとしましたが、この3つのデバイスでプログラムはうまくいっていました。 ありがとうございます。

関連する問題