2017-08-19 20 views
-1

自分の場所が変更されたときに通知します。 「FusedLocationApi.requestLocationUpdates」でユーザーの現在地を監視しています。Android、アプリがスワイプ/スキップされたときにサービスが停止する

アプリが存続する限り、すべて機能します。

しかし、私がアプリをスワイプ/キルすると、私はもはや通知を受け取りません。アプリを殺す/スワイプしても通知を受け続けるにはどうすればよいですか?

@Override 
public void onHandleIntent(Intent intent){ 

    googleApiClient = new GoogleApiClient.Builder(this) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .addApi(LocationServices.API) 
      .build(); 
    googleApiClient.connect(); 

    locationRequest = new LocationRequest(); 
    locationRequest.setInterval(2000); 
    locationRequest.setFastestInterval(1000); 
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
} 


@Override 
public void onConnected(Bundle bundle) { 
    try{ 
     LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this); 
    } catch (SecurityException ex){ex.printStackTrace();} 
} 

@Override 
public void onLocationChanged(Location location) { 
    this.location = location; 
    handleLocationUpdate(); 
} 

private void handleLocationUpdate(){ 
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(BackGroundService.this); 
    mBuilder.setSmallIcon(R.mipmap.ic_launcher); 
    mBuilder.setContentTitle(String.format(Locale.getDefault(), 
      "Lat:%.2f Long:%.2f", location.getLatitude(), location.getLongitude())); 
    mBuilder.setContentText("Hi, This is Android Notification Detail!"); 

    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    mNotificationManager.notify(1, mBuilder.build()); 
    //startForeground(1, mBuilder.build()); 
} 

答えて

1

が、これはアウトスワイプによる殺傷からウルサービスを保存しようとします。いくつかのカスタムROMのURアプリを殺すとしてもそれを使用する

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    return Service.START_STICKY; 
} 

タスクから削除?さて、私は再びそれを開始します

@Override 
public void onTaskRemoved(Intent rootIntent) { 
    super.onTaskRemoved(rootIntent); 
    Intent myIntent = new Intent(this, NotificationService.class); 
    startService(myIntent) 
} 

破棄?

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    //startService(new Intent(getApplicationContext(), NotificationService.class)); 

    Intent myIntent = new Intent(this, NotificationService.class); 

    PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, myIntent, 0); 

    AlarmManager alarmManager1 = (AlarmManager) getSystemService(ALARM_SERVICE); 

    Calendar calendar = Calendar.getInstance(); 

    calendar.setTimeInMillis(System.currentTimeMillis()); 

    calendar.add(Calendar.HOUR, 6); 

    alarmManager1.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); 

} 
+0

ありがとうございます。 **オーバーライドされたonStart' **メソッドを追加することでそれを動作させました。しかし、私は実現しました。** onStart **と** onHandleIntent **は一緒に演奏されません。だから私は** IntentService **基底クラスの代わりに** 'Service' **基底クラスから拡張しなければなりませんでした。 – QnARails

1

あなたのやりたいことに応じて、Androidサービスやインテリジェントサービスを利用する必要があります。その後、ユーザーが別のアプリを使用してもバックグラウンドで作業を実行できます。

サービスをバックグラウンドで実行し続ける必要があるため、サービスを利用する必要があると思います。 Intentserviceは、オペレーションが終了するまでのみ実行されます。

彼女のいくつかの読み取りmatirials:

IntentServiceServicesIntentService vs Serviceenter code here

関連する問題