从推出服务活动的通知被点击时通知

2023-09-12 05:11:03 作者:Dialog(对白)

我知道,有这些就在这里万吨,但我一直在努力解决了一整天,还没有得到任何地方。 无论是对谷歌的文档的例子,也没有任何的我已经在这里找到了5其他方式也都为我工作。 由于是典型的例子,当我点击该通知将关闭状态栏并没有什么新的屏幕上显示。 我建立从服务的通知,并需要通知来触发尚未创建的新活动。 我还需要一种方式来传递信息,意图通过这一活动。

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.

是的...这是java的Andr​​oid版

And yes... this is java for Android

接下来是我的code破碎的残迹。

What follows are the shattered remnants 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)
    {
        ...
    }

}

我只希望,无论是造成我的问题是可以解决的事情,而不是更疯狂的毛刺我已经得到使用Eclipse(这是任何其他人似乎见过>:U)的

I can only hope that whatever is causing my problem is something fixable and not more of the crazy glitches I've been getting with eclipse (which no one else seems to have ever seen >: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

推荐答案

编辑:

你将不得不添加的标记的为FLAG_ACTIVITY_NEW_TASK:

You're going to have to add a flag for FLAG_ACTIVITY_NEW_TASK:

nIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

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

This is because you're launching from outside your app (from the system notification bar).