START_STICKYを使用した後でも、サービスが常に実行されているわけではありません。
発信通話のトーストアクションが表示されないことがありますが、それはサービスがしばらくすると停止することを意味しますか?
アプリケーションは、ユーザーが電話から発信するたびにトーストを表示します。このために、BroadcastReceiverを使用して、呼び出しアクションとサービスをタップしています(Receiverを常に実行するため)。このアクティビティを開始すると、発信コールが開始されるとトーストが表示され始めますが、常にではありません。
以下は完全なコードです-
MainActivity.class
public class MainActivity extends Activity
{
CallNotifierService m_service;
boolean isBound = false;
private ServiceConnection m_serviceConnection = new ServiceConnection()
{
@Override
public void onServiceConnected(ComponentName className, IBinder service)
{
m_service = ((CallNotifierService.MyBinder)service).getService();
Toast.makeText(MainActivity.this, "Service Connected", Toast.LENGTH_LONG).show();
isBound = true;
Intent intent = new Intent(MainActivity.this, CallNotifierService.class);
startService(intent);
}
@Override
public void onServiceDisconnected(ComponentName className)
{
m_service = null;
isBound = false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, CallNotifierService.class);
bindService(intent, m_serviceConnection, Context.BIND_AUTO_CREATE);
}
.
.
.
}
CallNotifierService.class
public class CallNotifierService extends Service
{
private final IBinder myBinder = new MyBinder();
private static final String ACTION_OUTGOING_CALL = "android.intent.action.NEW_OUTGOING_CALL";
private CallBr br_call;
@Override
public IBinder onBind(Intent arg0)
{
return myBinder;
}
@Override
public void onDestroy()
{
Log.d("service", "destroy");
this.unregisterReceiver(this.br_call);
Toast.makeText(CallNotifierService.this, "Receiver Un-Registered", Toast.LENGTH_LONG).show();
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
final IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_OUTGOING_CALL);
this.br_call = new CallBr();
this.registerReceiver(this.br_call, filter);
Toast.makeText(CallNotifierService.this, "onStartCommand Called", Toast.LENGTH_LONG).show();
return START_STICKY;
}
public class MyBinder extends Binder
{
CallNotifierService getService()
{
return CallNotifierService.this;
}
}
public class CallBr extends BroadcastReceiver
{
public CallBr() {}
@Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "Action:"+intent.getAction(), Toast.LENGTH_LONG).show();
}
}
}
ここでは、単純なアイデア(正しく実行されれば機能する)とより複雑なアイデア(機能しない)を組み合わせることで、間違ったアプローチをとっています。
注意:START_STICKYを使用している場合でも、サービスは「常に実行中」のコンポーネントではありません。
Androidシステムは、どこか別の場所にメモリが必要な場合、サービスを強制終了することを躊躇しません。START_STICKYは、ドキュメントで指定されているようにonStartCommandを呼び出して、Androidシステムが可能な場合にサービスを再起動することを意味するだけです。
本当に固執するサービスが必要な場合は、フォアグラウンドサービスを使用する必要があります。ただし、UI(迷惑な通知アイコンが常に表示される)とバッテリー寿命に影響するため、ここでは必要ありません。
これが手品です。BroadcastReceiverが機能するためにアプリを実行する必要はありません。あなたがする必要があるのは正しいintent-filterであなたのAndroidManifest.xmlにそれを登録することです:
<receiver android:name=".broadcastreceivers.CallBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
(また、アプリに必要な権限、つまりPROCESS_OUTGOING_CALLSがあることを確認してください)。
次に、コードに必要なのは次のとおりです。
public class CallBroadcastReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "Action: " + intent.getAction(), Toast.LENGTH_LONG).show();
}
}
アクティビティなし(Android 6以降でPROCESS_OUTGOING_CALLS権限を要求する場合を除く)、サービスなし、何もありません。シンプルでバッテリー効率が良い!
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加