私はまた、サービスでPhoneStateListenerを実行しようと、この問題があったが、それが唯一のN・デバイス上だけで「ドロップ」のと同じように思えます。
私は多くの異なる提案を試みましたが、7.0を実行しているnexus5xでリスナーを失い続けました。時間の100%を生きていける方法は、サービスにフォアグラウンド通知を実行させることでした。基本的には、サービスが生存している限り通知トレイには存続します。これは、発信コールが応答されたときのように、以前のリスナーを削除するさまざまな電話状態を通じてサービスを維持します。通知ビルダにサウンドを設定したり、振動を与えたりしない限り、目立たないほどです。このように見えるのonCreateマイサービス:
MyPhoneListener phoneStateListener = new MyPhoneListener(getApplicationContext());
TelephonyManager telephonymanager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
telephonymanager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
Intent notificationIntent = new Intent(this, YourActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_favorites) //status bar icon
.setContentTitle("Title") //whatever title
.setContentText("Stuff") //main notification text
.setContentIntent(pendingIntent).build();
startForeground(12345, notification);
、その後、あなたがサービスのonDestroyで通知を削除します。このことができます
@Override
public void onDestroy() {
Log.i("log", "service ending");
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.cancel(12345);
}
願っています!
ヌガットでこのような設定を見つけることができません – PPB