How to start activity when user clicks a notification?

android, android-activity, java, notifications

Solution

I figured out the problem. I forgot to include the package name in the activity declaration in the Manifest file.

Wrong:

activity android:name=".ViewTarget" android:label="@string/app_name" 

Correct:

activity android:name="com.kjdv.gpsVegas.ViewTarget" android:label="@string/app_name" 

Problem

I am attempting to convert some code I found in a tutorial for my own use. Originally, the code launched the system contacts list when the user would click a notification generated by my app. I am trying to start an `Activity` of my own instead of launching the contact list, but it's not working. More specifically, nothing happens. There is no error, and my `Activity` doesn't load either. The notification window disappears after clicking, and the original `Activity` is still visible. Here is my code: ``` public class MyBroadcastReceiver extends BroadcastReceiver { private NotificationManager mNotificationManager; private int SIMPLE_NOTFICATION_ID; public void onReceive(Context context, Intent intent){ Bundle extras = intent.getExtras(); String deal = (String) extras.get("Deal"); String title = "Deal found at " + (String) extras.get("LocationName"); mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); Notification notifyDetails = new Notification(R.drawable.icon, title,System.currentTimeMillis()); Class ourClass; try { ourClass = Class.forName("com.kjdv.gpsVegas.ViewTarget"); Intent startMyActivity = new Intent(context, ourClass); PendingIntent myIntent = PendingIntent.getActivity(context, 0,startMyActivity, 0); notifyDetails.setLatestEventInfo(context, title, deal, myIntent); notifyDetails.flags |= Notification.FLAG_AUTO_CANCEL; notifyDetails.flags |= Notification.DEFAULT_SOUND; mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } ``` This is my entry in the `AndroidManifext.xml` file... ``` <activity android:name=".ViewTarget" android:label="@string/app_name" > <intent-filter> <action android:name="com.kjdv.gpsVegas.ViewTarget" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> ``` And this is my `Activity` that I want to launch... ``` public class ViewTarget extends ListActivity { public ListAdapter getListAdapter() { return super.getListAdapter(); } public ListView getListView() { return super.getListView(); } public void setListAdapter(ListAdapter adapter) { super.setListAdapter(adapter); } protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.locations); Log.v("db", "Inside ViewTarget"); } } ```

Original source