简体   繁体   English

待定意图未打开活动

[英]Pending Intent is not opening activity

I'm new to Android development. 我是Android开发的新手。 I'm able to see the ticker message "Did you reach to office", but when I click it, it's not opening my main activity. 我可以看到自动收录机消息“您到达办公室了吗”,但是当我单击它时,它没有打开我的主要活动。

Below is my code: 下面是我的代码:

private class ViewUpdater implements Runnable{
  private Context contextFromView;
  public ViewUpdater(Context context) {
    this.contextFromView = context;
  }
  @Override
  public void run() {
    CancelNotification(contextFromView, notificationId);
    Random r=  new Random();
    notificationId =r.nextInt();
    Intent intent = new Intent();
    FragmentActivity activity = getActivity();
    PendingIntent pi = PendingIntent.getActivity(getActivity(),1000,intent,0);
    String body = "congratulations you made it today";
    String title = "Office reached";
    NotificationCompat.Builder mBuilder =
      new NotificationCompat.Builder(getActivity())
      .setSmallIcon(R.drawable.abc_ic_go)
      .setContentTitle(title)
      .setTicker("Did you reach office?")
      .setContentText(body);
    mBuilder.setContentIntent(pi);
    nm.notify(notificationId, mBuilder.build());
    timerHandler.postDelayed(this, 200000);
  }
}

You have to give an Intent to start the right Activity when creating your PendingIntent . 创建PendingIntent时,您必须提供一个Intent以启动正确的Activity。 Also the FLAG_ACTIVITY_NEW_TASK is necessary because you'll start the Activity outside the context of an existing Activity (source: Android doc ). 另外,还需要FLAG_ACTIVITY_NEW_TASK ,因为您将在现有Activity的上下文外部启动Activity(来源: Android doc )。

Intent = new Intent(getActivity(), YourActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pi = PendingIntent.getActivity(getActivity(),1000,intent,0);

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

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