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

dar*_*rsh 2 service notifications android 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));

        }
    });
Run Code Online (Sandbox Code Playgroud)

这是我服务中的onStartCommand方法.

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

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

}
Run Code Online (Sandbox Code Playgroud)

我在待定意图中设置了活动B(TestActivity).根据我的需要显示通知,但是当我点击通知时,活动未启动.

我在清单中声明了这项服务.

<service android:name=".Service_class" />
Run Code Online (Sandbox Code Playgroud)

我应该在清单中声明其他什么吗?可能是什么问题呢 ?

Way*_*yne 8

您已声明TestActivity.classAndroidManifest.xml

  • 我的错!谢谢你指出.我总是犯愚蠢的错误. (2认同)