2

如何从服务执行 Activity 的 onNewIntent()?触发意图时要给出什么标志?

4

2 回答 2

3

你不能自己调用onNewIntent​​它会被系统调用。如果您的活动可以处理触发的意图,那么它将自动调用,检查您的意图和相关的意图过滤器。

仅当您的活动是单顶活动并且您的活动的 Oncreate 已被调用时才会调用。

于 2012-09-14T06:37:18.187 回答
1

例如,如果您想为通知做一些事情,则必须覆盖此方法

    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
}
于 2012-09-14T07:36:14.343 回答