2017-01-15 27 views
1

私は実際には親が自分の子供を追跡できるAndroidアプリケーションを開発しています。私が直面している問題は、GPSの場所はN秒ごとに実行する必要がありますが、強制的にアプリケーションを閉じるか、モバイルを再起動してもサービスは再起動しません。ここでAndroidで強制終了して再起動するサービス

は私のLocationServiceです:

public class LocationService extends Service 
{ 
    private LocationListener listener; 
    private LocationManager manager; 

    @Nullable 
    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 

    @Override 
    public void onCreate() 
    { 
     super.onCreate(); 
     Toast.makeText(this, "Service.onCreate()", Toast.LENGTH_LONG).show(); 
    } 

    @Override 
    public void onDestroy() 
    { 
     super.onDestroy(); 
     Toast.makeText(this, "Service.destroy()", Toast.LENGTH_LONG).show(); 
     if(this.manager != null) 
      //noinspection MissingPermission 
      this.manager.removeUpdates(this.listener); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flag, int startId) 
    { 
     super.onStartCommand(intent, flag, startId); 

     //Here end the code to execute 
     Toast.makeText(this, "Service.onStartCommand()",Toast.LENGTH_LONG).show(); 

     this.listener = new LocationListener() { 
      @Override 
      public void onLocationChanged(Location location) { 
       Log.e("started", "service running"); 
       play(); 
       Toast.makeText(LocationService.this, location.getLongitude() + " " + location.getLatitude(), Toast.LENGTH_SHORT).show(); 
      } 

      @Override 
      public void onStatusChanged(String s, int i, Bundle bundle) { 

      } 

      @Override 
      public void onProviderEnabled(String s) { 

      } 

      @Override 
      public void onProviderDisabled(String s) { 
       Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
       intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       startActivity(intent); 
      } 
     }; 

     this.manager = (LocationManager)getApplicationContext().getSystemService(Context.LOCATION_SERVICE); 

     //noinspection MissingPermission 
     this.manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 3000,0, this.listener); 

     return START_NOT_STICKY; 
    } 

    @Override 
    public boolean onUnbind(Intent intent) 
    { 
     // TODO Auto-generated method stub 
     Toast.makeText(this, "Service.onUnbind()", Toast.LENGTH_LONG).show(); 
     return super.onUnbind(intent); 
    } 
} 

マイ放送受信機:

public class BootIntentReceiver extends BroadcastReceiver 
{ 
    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
     Intent serviceIntent = new Intent(context, LocationService.class); 
     serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     context.startService(serviceIntent); 
    } 
} 

マイたManifest.xml:ブート用

<receiver android:name=".activity.service.BootIntentReceiver"> 
    <intent-filter> 
     <action android:name="android.intent.action.BOOT_COMPLETED" /> 
    </intent-filter> 
</receiver> 
<service 
    android:name=".activity.service.LocationService" 
    android:exported="false" /> 

許可:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 

私はデバイスを起動しても何も起こりません:/

あなたの助けをいただきありがとうございます!

答えて

1

マニフェストでサービスの完全修飾パッケージ名を使用しようとしましたか?

サービスはcom.package.activity.serviceパッケージ内だけではなく、<receiver android:name=".activity.service.BootIntentReceiver">

の使用<receiver android:name="com.package.activity.service.BootIntentReceiver">ですそして、ちょうど

+0

二重に確認するために、あなたのBootIntentReceiverにログやトーストを入れた場合には、実際に動作しませんでした!心が吹っ飛んでO!私は何時間もエラーを検索しようとして過ごしましたが、それはちょうどそれでした..笑。 とにかく!どうもありがとうございました ! また、一定期間後に停止するのはなぜですか? – IgZiStO

関連する問題