gpt4 book ai didi

android - 未决 Intent 在服务创建的通知中不起作用

转载 作者:太空狗 更新时间:2023-10-29 15:55:25 28 4
gpt4 key购买 nike

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

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

}

我正在挂起的 Intent 中设置 Activity B (TestActivity)。通知会根据需要显示,但是当我单击通知时, Activity 未启动。

我在 list 中声明了该服务。

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

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

最佳答案

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

关于android - 未决 Intent 在服务创建的通知中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12136519/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com