待处理的意图不在服务创建的通知中工作

时间:2012-08-27 05:12:50

标签: android service notifications android-pendingintent

我有两个活动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" />

我是否应该在清单中声明其他内容?可能是什么问题?

1 个答案:

答案 0 :(得分:8)

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