简体   繁体   English

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

[英]How to call onNewIntent() of Activity from a service

How to execute onNewIntent() of Activity from a sevice? 如何从服务执行Activity的onNewIntent()? What are flags to be given while firing the intent?? 激发意图时应给出哪些标志?

You cannot call onNewIntent by self it'll called by the system. 您不能自行调用onNewIntent ,它会被系统调用。 If your activity can handle fired intent then it will be called automatically, check your intent and related intent filter. 如果您的活动可以处理触发的意图,那么它将被自动调用,请检查您的意图和相关的意图过滤器。

This is only called when you activity is singletop and your activity's Oncreate has already been called. 仅当您的活动为singletop并且活动的Oncreate已被调用时才调用此方法。

You will have to override this method if you want to do some thing for notification as an example 如果您想做一些通知的事情,您将必须重写此方法。

    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);

and my DemoActivity.class looks likes 我的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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM