【问题标题】:pending intent not working in notification created by a service待处理的意图在服务创建的通知中不起作用
【发布时间】:2012-08-27 05:12:49
【问题描述】:

我有两个活动 A 和 B。A 在应用程序启动时启动。我有一个从 A 启动的服务。

这是我在活动 A 中的代码。

Button btnStart = (Button) findViewById(R.id.button1);
    btnStart.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            startService(new Intent(getBaseContext(), Service_class.class));

        }
    });

这是我的服务中的 onStartCommand 方法。

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    createNotification();
    Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
    return START_STICKY;
}

这里是 createNotification 方法

private void createNotification() {

    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Intent nintent = new Intent();
    nintent.setClass(this, TestActivity.class);
    PendingIntent pin = PendingIntent.getActivity(getApplicationContext(),
            0, nintent, 0);
    String title = "KR Darsan";
    String body = "This is a notification from darsh";
    Notification n = new Notification(R.drawable.ic_launcher, body,
            System.currentTimeMillis());
    n.contentIntent = pin;
    n.setLatestEventInfo(getApplicationContext(), title, body, pin);

    n.defaults = Notification.DEFAULT_ALL;
    nm.notify(unique_id, n);

}

我在未决意图中设置了活动 B (TestActivity)。通知会根据需要显示,但是当我单击通知时,活动不会启动。

我在清单中声明了服务。

<service android:name=".Service_class" />

还有什么我应该在清单中声明的​​吗?可能是什么问题?

【问题讨论】:

    标签: android service notifications android-pendingintent


    【解决方案1】:

    您是否在AndroidManifest.xml 中声明了TestActivity.class

    【讨论】:

    • 我的错!感谢您指出。我总是犯愚蠢的错误。
    • 你是说 TestActivity.class 应该列在两个清单中吗?即,在活动 A 和活动 B 的清单中?
    • Activity 是应用程序的一个窗口,而不是应用程序本身。应用或库项目只能有一个清单。
    猜你喜欢
    • 2014-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多