English 中文(简体)
通知被点击的发射活动
原标题:launch activity from service when notification is clicked

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

以下是我法典的破碎残余物。

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)
    {
        ...
    }

}

我只能希望,造成我问题的任何事情都是可以解决的,而不是我所 的摇篮(似乎没有人看到过这种lip;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

谢谢:D

问题回答

<><>Edit>:

页: 1 ACTIVITY_NEW_TASK:

nIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

这是因为你从你之外(从系统通知栏)重新启动。

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

www.un.org/Depts/DGACM/index_spanish.htm 联系人Backup(服务班),

   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);

<>接触值 Viewer category,

页: 1

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




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签