单击通知时从服务启动活动

use*_*058 6 service notifications android launch android-activity

我知道,这里有很多这些,但我一直在尝试解决方案而且没有得到任何地方.谷歌文档上的例子,以及我在这里找到的其他5种方式都没有对我有用.与典型情况一样,当我单击通知时,它会关闭状态栏,屏幕上不会显示任何新内容.我正在从服务创建通知,并且需要通知才能触发尚未创建的新活动.我还需要一种通过意图将信息传递给该活动的方法.

是的......这是适用于Android的java

以下是我的代码中破碎的残余.

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

}
Run Code Online (Sandbox Code Playgroud)

我只能希望无论是什么导致我的问题是可以修复的,而不是更多我曾经在日食中遇到的疯狂故障(其他人似乎没见过):U)

如果你能帮我解决这个问题,请分享.如果您无法解决这个具体问题,但觉得有必要说出关于发布,样式,主题或良好实践的不相关的事情,那么请不要

谢谢你:D

kab*_*uko 6

编辑:

你将不得不为FLAG_ACTIVITY_NEW_TASK 添加一个标志:

nIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Run Code Online (Sandbox Code Playgroud)

这是因为您从应用程序外部(从系统通知栏)启动.