launch activity from service when notification is clicked

℡╲_俬逩灬. 提交于 2020-01-02 03:36:29

问题


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.

And yes... this is java for Android

What follows are the shattered remnants of my 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)
    {
        ...
    }

}

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 )

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

Thank you :D


回答1:


Edit:

You're going to have to add a flag for FLAG_ACTIVITY_NEW_TASK:

nIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

This is because you're launching from outside your app (from the system notification bar).




回答2:


This is what happens when people overwork themselves. XD The only reason none of the tutorials I tired worked is because I misspelled my activity name in the manifest. Thanks for stopping by




回答3:


Just add following in contactBackup(service class),

   Intent nIntent = new Intent(this,contactViewer.class);//Intent nIntent = new Intent(Intent.ACTION_MAIN);
        nIntent.setClass(context,contactViewer.class);
         nIntent.putExtra("data",data);

  nIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        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);

then get value in contactViewer class,

as,

String s=getIntent().getStringExtra("data");


来源:https://stackoverflow.com/questions/7973786/launch-activity-from-service-when-notification-is-clicked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!