0

私は、アクティビティから開始してバインドするサービスを持っています。アクティビティのOnDestroyメソッドでは、サービスにバインドを解除します。サービス通知を表示し、startForeground()を使用してフォアグラウンドに配置します。サービスのonUnbind()メソッドでは、stopself()とstopForeground(true)を呼び出します。しかし、サービスが機能しなくなっているようです。アプリは終了し、通知は消えていますが、アンドロイドスタジオのアンドロイドメモリモニターは継続しています。なぜこうなった? stopForeground()を呼び出すサービスで関数を作成し、アクティビティのOnDestroyの前に呼び出すと、アプリケーションは正常に終了します。メモリーモニターが停止します。 OnCreate関数でAndroidサービスMarshmallowで止まらない

MainActivity():OnDestroyで

service = new Intent(MainActivity.this, Service.class); 


    service.setAction("START"); 
    startService(service); 

    if(myservice == null) 
    { 
     Intent k = new Intent(this,Service.class); 
     bindService(k, myConnection, Context.BIND_AUTO_CREATE); 
    } 

MainActivity()

  unbindService(myConnection); 

サービスOnCreate関数()

Intent notificationIntent = new Intent(this, MainActivity.class); 
     notificationIntent.setAction("new"); 
     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK 
       | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, 
       notificationIntent, 0); 


     Bitmap icon = BitmapFactory.decodeResource(getResources(), 
       R.drawable.ic_launcher); 

     Notification notification = new NotificationCompat.Builder(this) 
       .setContentTitle("Myservice") 
       .setTicker("service") 
       .setContentText("Monitoring") 
       .setSmallIcon(R.drawable.ic_launcher) 
       .setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false)) 
       .setContentIntent(pendingIntent) 
       .setOngoing(true).build(); 

     startForeground(101, notification); 
+0

「しかし、アンドロイドスタジオのアンドロイドメモリモニターは続ける」 - 私はあなたに何を「続ける」かを知らない。あなたがサービスを停止しただけで、あなたのプロセスは遠ざかりません。 [プロセスは引き続き実行されます](http://developer.android.com/guide/components/processes-and-threads.html#Processes)。同様に、自分がフォークしたスレッドなど、設定した他の進行中の作業は引き続き実行されます。あなたのサービスがこの種のことをしたなら、あなたは 'onDestroy()'やその他の可能性のある点でその情報を整理する必要があります。 – CommonsWare

+0

@ CommonsWare-なぜ、この問題は通知とStartForground()を使用した場合にのみ発生しますか? – amanda45

+0

"この問題"が何であるかわからないので、私はそれに答えることはできません。あなたの質問を編集し、「アンドロイドスタジオのアンドロイドスタジオでのアンドロイドメモリモニタの継続」の意味を詳しく説明してください。 – CommonsWare

答えて

0

1)サービスは、バックグラウンドのでonDestroy()方法で実行されていますメソッドを呼び出して呼び出す必要があります。

stopSelf(); 

2)あなたは、あなたが必要とする活動の後、onDestroy()方法をあなたは)onTaskRemoved()を実装し、

stopSelf(); 

3を呼び出す必要がありますそして、あなたが活動閉じる上のサービスを停止したい近いアプリ上でサービスを閉じたいですフォローコードを呼び出す

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    Intent serviceIntent=new Intent(this,ServiceClass.class); 
    stopService(serviceIntent); 
} 
関連する問題