我有两个活动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" />
我应该在清单文件中声明其他内容吗?可能出了什么问题?