当通知被点击时,从服务启动活动

6
我知道,这里已经有很多类似的内容了,但是我一整天都在尝试解决方案,却没有任何进展。 既不是谷歌文档上的示例,也不是这里的其他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)
    {
        ...
    }

}

我只希望引起问题的原因是可以修复的,而不是我一直在使用eclipse时遇到的疯狂故障(似乎没有其他人见过)。如果您可以帮助我解决这个问题,请分享。如果您无法帮助解决此特定问题但感到有义务发表关于发布、样式、主题或良好实践的无关言论,则请不要。谢谢:D
3个回答

6

编辑:

您需要添加一个FLAG_ACTIVITY_NEW_TASK标记:

nIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

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

好的,那不是你正确使用 putExtra 的方式... 但基于你的建议,我尝试将该标志添加到 Intent 和 PendingIntent 中,但都没有任何改变。不过还是谢谢。 - user980058

0

只需在contactBackup(服务类)中添加以下内容:

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

然后在contactViewer类中获取值,

如下所示,

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

0

这就是人们过度工作的后果。XD 我尝试的所有教程都没有起作用的唯一原因是我在清单文件中拼错了活动名称。 感谢您的光临。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接