如何从服务中执行活动的onnewintent()?发射意图时要给什么旗子?是吗?

最佳答案

如果你想做一些事情,你必须重写这个方法
以通知为例

    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
}

关于android - 如何从服务中调用Activity的onNewIntent(),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12419094/

10-12 00:10