0

我正在编写一个前台服务来接收 GPS 通知、发送系统通知和拨打电话。该应用程序没有Activity附加到它,只有一个从引导接收器启动的服务。当我试图从服务的 onLocationChanged() 中启动调用活动时,我得到:

从 Activity 上下文外部调用 startActivity() 需要 FLAG_ACTIVITY_NEW_TASK 标志。这真的是你想要的吗?

害怕这个怀疑的问题,我决定看看 stackOverFlow,在那里我发现了这些: Calling startActivity() from outside of an Activity contextAndroid: Make phone call from serviceandroid 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);
    }

    ...
}
4

1 回答 1

2

答案完全在于用户体验 ( UX ) 领域。Android 设备通常是个人设备,在编写应用程序时应牢记这一点。

用户可能正在玩游戏或打电话,在没有任何通知的情况下启动您的活动是不礼貌的,我会卸载任何会这样做的应用程序。

此外,如果手机被锁定,您的活动将不会真正启动,而是会等到用户解锁手机。

另一方面,通知是为了告诉用户该应用程序想要向您展示一些东西。因此,请改用它们。

除非您正在构建一个私有应用程序,否则您知道什么更适合您的要求。

于 2013-02-25T09:08:23.577 回答