[英]launch activity from service when notification is clicked
我知道,这里有很多这些,但我一直在尝试解决方案而且没有得到任何地方。 谷歌文档上的例子,以及我在这里找到的其他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
编辑:
你将不得不为FLAG_ACTIVITY_NEW_TASK添加一个标志 :
nIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
这是因为您从应用程序外部(从系统通知栏)启动。
只需在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");
当人们过度劳累时会发生这种情况。 XD我厌倦了没有教程的唯一原因是因为我在清单中拼错了我的活动名称。 谢谢你的光临
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.