I have two activities A and B. A is launched when the application is started. I have a service which is launched from A.
here’s my code in 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));
}
});
Here’s the onStartCommand method in my service.
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
createNotification();
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
return START_STICKY;
}
here’s the createNotification method
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);
}
I am setting the Activity B (TestActivity) in the pending intent. Notification shows up as I need but when I click on the notification, the activity is not launched.
I declared the service in the manifest.
<service android:name=".Service_class" />
Is there something else I should declare in the manifest ? What could be the problem ?
Have you declared
TestActivity.classinAndroidManifest.xml?