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

时间:2011-11-01 22:34:38

标签: android service android-activity notifications launch

我知道,这里有很多这些,但我一整天都在尝试解决方案而且没有任何进展。 谷歌文档上的例子,以及我在这里找到的其他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)
    {
        ...
    }

}

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

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

谢谢:D

3 个答案:

答案 0 :(得分:6)

修改

您将不得不为FLAG_ACTIVITY_NEW_TASK添加标志

nIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

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

答案 1 :(得分:0)

当人们过度劳累时会发生这种情况。 XD 我厌倦的教程中没有一个工作的唯一原因是因为我在清单中拼错了我的活动名称。 谢谢你停下来

答案 2 :(得分: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");