【问题标题】:Calling an Activity from a Service从服务调用活动
【发布时间】:2012-06-24 00:20:00
【问题描述】:

我有一个包含服务方法的警报服务的服务类。这些方法在警报服务被激活时被调用。我想要做的是在服务类中调用的这些方法之一中调用另一个类的意图(当警报响起时)。它所做的只是在调用意图时标记错误。这只发生在激活警报服务时调用的方法中(服务类中的方法)。这是因为类extends Service 而不是extends Activity?我不确定,有什么好办法吗?

(下面是我的服务类,当在onStart 方法中调用另一个活动的意图时,应用程序强制关闭。)

public class MyAlarmService extends Service {
    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG).show();
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG).show();
        return null;
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        // TODO Auto-generated method stub
        super.onStart(intent, startId);

        Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show();

        //////////////////////////////////////////////////////////////////////////////////

        Intent i = new Intent("com.exercise.AndroidAlarmService.HELLO"); 
        startActivity(i); 

        The intent that is send to open another class, an activity.
        **

        /////////////////////////////////////////////////////////////////////////////////
    }

    @Override
    public boolean onUnbind(Intent intent) {
        // TODO Auto-generated method stub
        Toast.makeText(this, "MyAlarmService.onUnbind()", Toast.LENGTH_LONG).show();
        return super.onUnbind(intent);
    }
}

LogCat 上的这些错误之一是:

06-24 01:11:36.857: E/AndroidRuntime(10805): java.lang.RuntimeException: Unable to start service com.exercise.AndroidAlarmService.MyAlarmService@412f23f8 with Intent { flg=0x4 cmp=com.exercise. AndroidAlarmService/.MyAlarmService (has extras) }: android.util.AndroidRuntimeException: 从 Activity 上下文之外调用 startActivity() 需要 FLAG_ACTIVITY_NEW_TASK 标志。这真的是你想要的吗?

【问题讨论】:

    标签: android service android-intent


    【解决方案1】:

    您是否尝试过错误日志的提示?

    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    

    【讨论】:

      【解决方案2】:

      您可以使用服务的onStart() 调用活动.....

      @Override 
      public void onStart(Intent intent, int startId)  { 
          ...
          Log.i("Service", "onStart() is called"); 
          Intent callIntent = new Intent(Intent.ACTION_CALL); 
          callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
          callIntent.setClass(<Set your package name and class name here>);
          startActivity(callIntent);
          ...
      
      }
      

      【讨论】:

        【解决方案3】:

        您可以通过启用其他人建议的标志来做到这一点。默认情况下阻止它的原因是因为服务很容易在后台被系统自动重启。如果您在服务的 onStart 期间启动活动,则无论用户可能在做什么,此活动都会启动。这将是糟糕的用户体验。请牢记这一警告,并针对这种情况进行变通。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-12-01
          • 1970-01-01
          • 2019-02-27
          • 2014-06-28
          • 2015-06-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多