GPS 通知を受信し、システム通知を送信し、電話をかけるためのフォアグラウンド サービスを作成しています。アプリケーションには何もActivity
接続されておらず、ブート レシーバーから起動されるサービスのみが接続されています。サービスの onLocationChanged() 内から呼び出しアクティビティを開始しようとしたときに、次のようになりました。
Activity コンテキストの外部から startActivity() を呼び出すには、FLAG_ACTIVITY_NEW_TASK フラグが必要です。これは本当にあなたが望むものですか?
懐疑的な質問を恐れて、私は stackOverFlow を見ることにしました。そこで私はこれらを見つけました: Calling startActivity() from outside of an Activity context , Android: Make phone call from service , android start activity from service - すべてがこの正確なことを行うことを示唆しています.
だから、私の質問は: なぜこのフラグ (履歴スタックに関するもの) を使用することはお勧めできませんか? 私の場合、それをしても大丈夫ですか?
簡略化されたコード:
public class CallService extends Service implements LocationListener {
@Override
public void onCreate() {
super.onCreate();
startForeground(1, NotificationGenerator.getNotification());
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public synchronized void onLocationChanged(Location loc) {
String url = "tel:xxxxxxxxxx";
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
...
}