如何从服务中调用Activity的onNewIntent()

时间:2012-09-14 06:18:18

标签: android android-intent

如何从服务执行Activity的onNewIntent()?在触发intent时会给出什么标志??

2 个答案:

答案 0 :(得分:3)

您无法通过系统调用自己调用onNewIntent。如果您的活动可以处理已触发的意图,则会自动调用它,检查您的意图和相关意图过滤器。

只有当您的活动是单一的并且您的活动的Oncreate已被调用时,才会调用此方法。

答案 1 :(得分:2)

如果你想做某件事,你将不得不重写这个方法     以通知为例

    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(com.example.xyz.R.drawable.ic_launcher,message1, when);

    Intent notificationIntent = new Intent(context,com.example.xyz.DemoActivity.class);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_SINGLE_TOP);
    notificationIntent.putExtra("MESSAGE",pkgName);
    notificationIntent.putExtra("APP_STATUS", AppStatus);
    notificationIntent.putExtra("APP_NAME",AppName);
    //PendingIntent.FLAG_UPDATE_CURRENT will update the notification 
    PendingIntent intent=PendingIntent.getActivity(context, 0, notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT );
    notification.setLatestEventInfo(context, title, message1, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(0, notification);

我的DemoActivity.class看上去很像

       public class DemoActivity extends Activity{

 public static String BROADCAST_ACTION = "com.example.android.APP_CLOUD_DELETE_APK";
 private TextView  messsageText,NotificationHeader;
 private Button okButton;
 private int AppStatusId;
 private String PkgName,app_name,ApkFileName;
 private RegisterTask mRegisterTask;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.demo_activity);
    //  if this activity is not  in stack , this mwthod will be called


}

@Override
protected void onNewIntent(Intent intent) {
    // TODO Auto-generated method stub
    // if this activity is in stack , this mwthod will be called
}