Note that there are some explanatory texts on larger screens.

plurals
  1. POlaunch activity from service when notification is clicked
    text
    copied!<p>I know, there are tons of these on here, but I've been trying solutions all day and haven't gotten anywhere. Neither the example on google's docs, nor any of the 5 other ways I've found on here have worked for me at all. As is the typical case, when I click the notification it closes the status bar and nothing new is shown onscreen. I am creating the notification from a service and need the notification to trigger a new activity that has not yet been created. I also will need a way to pass information to that activity via intent.</p> <p>And yes... this is java for Android</p> <p>What follows are the shattered remnants of my code.</p> <pre><code>package com.bobbb.hwk2; import java.io.FileOutputStream; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.IBinder; import android.provider.ContactsContract; import android.widget.Toast; public class contactBackup extends Service { private NotificationManager nManager; private static final int NOTIFY_ID = 1100; @Override public void onCreate() { super.onCreate(); String ns = Context.NOTIFICATION_SERVICE; nManager = (NotificationManager)getSystemService(ns); } @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); // inform user that service has started Toast.makeText(getApplicationContext(), R.string.service_started,Toast.LENGTH_LONG).show(); String data = lookUpContacts(); if( saveToSDCard(getResources().getString(R.string.backup_file_name),data) ) { Context context = getApplicationContext(); // create the statusbar notification Intent nIntent = new Intent(this,contactViewer.class);//Intent nIntent = new Intent(Intent.ACTION_MAIN); nIntent.setClass(context,contactViewer.class); //nIntent.putExtra("data",data); Notification msg = new Notification(R.drawable.icon,"All contacts records have been written to the file.",System.currentTimeMillis()); // start notification //PendingIntent pIntent = PendingIntent.getService(getApplicationContext(),0,nIntent,PendingIntent.FLAG_UPDATE_CURRENT|Intent.FLAG_FROM_BACKGROUND); PendingIntent pIntent = PendingIntent.getActivity(this,0,nIntent,0); msg.flags = Notification.FLAG_AUTO_CANCEL; msg.setLatestEventInfo(context, "success", "All contacts records have been written to the file.", pIntent); nManager.notify(NOTIFY_ID,msg); } } @Override public void onDestroy() { nManager.cancel(NOTIFY_ID); super.onDestroy(); } @Override public IBinder onBind(Intent intent) { return null; } // function returns string containing information // from contacts public String lookUpContacts() { ... } public boolean saveToSDCard(String fileName, String data) { ... } } </code></pre> <p>I can only hope that whatever is causing my problem is something fixable and not more of the crazy glitches I've been getting with eclipse (which no one else seems to have ever seen >:U )</p> <p>If you can help me solve this problem, please share. If you can't help with this specific problem but feel obligated to say unrelated things about posting, styles, topics, or good practice, then DON'T</p> <p>Thank you :D</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload