Start work from service when clicking on notification

I know there are a lot of them, but I tried all the time and I’m not going anywhere. None of the examples in google docs, none of the 5 other ways that I found here worked for me at all. As in the typical case, when I press the notification, it closes the status bar and nothing is displayed on the screen. I am creating a notification from a service and need a notification to initiate a new action that has not yet been created. I will also need a way to convey information to this activity through intent.

And yes ... it's Java for Android

What follows is the broken remains 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)
    {
        ...
    }

}

, , , - - , , ( -): U)

, . , , , ,

: D

+5
3

Edit:

FLAG_ACTIVITY_NEW_TASK:

nIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

- , ( ).

+6

, , . XD , , , - , . ,

0

Just add the following to contactBackup (class of service),

   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 the value in the contactViewer class,

and,

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

All Articles