gpt4 book ai didi

java - 用户单击通知时如何启动 Activity ?

转载 作者:IT老高 更新时间:2023-10-28 23:40:05 27 4
gpt4 key购买 nike

我正在尝试转换在教程中找到的一些代码供我自己使用。最初,当用户单击我的应用生成的通知时,代码会启动系统联系人列表。我正在尝试启动自己的 Activity 而不是启动联系人列表,但它不起作用。更具体地说,没有任何反应。没有错误,我的 Activity 也没有加载。点击后通知窗口消失,原来的Activity依然可见。

这是我的代码:

public class MyBroadcastReceiver extends BroadcastReceiver {
private NotificationManager mNotificationManager;
private int SIMPLE_NOTFICATION_ID;

public void onReceive(Context context, Intent intent){
Bundle extras = intent.getExtras();

String deal = (String) extras.get("Deal");
String title = "Deal found at " + (String) extras.get("LocationName");

mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notifyDetails = new Notification(R.drawable.icon, title,System.currentTimeMillis());

Class ourClass;
try {
ourClass = Class.forName("com.kjdv.gpsVegas.ViewTarget");
Intent startMyActivity = new Intent(context, ourClass);

PendingIntent myIntent = PendingIntent.getActivity(context, 0,startMyActivity, 0);
notifyDetails.setLatestEventInfo(context, title, deal, myIntent);
notifyDetails.flags |= Notification.FLAG_AUTO_CANCEL;
notifyDetails.flags |= Notification.DEFAULT_SOUND;
mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}

这是我在 AndroidManifext.xml 文件中的条目...

  <activity android:name=".ViewTarget" android:label="@string/app_name" >
<intent-filter>
<action android:name="com.kjdv.gpsVegas.ViewTarget" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

这是我要启动的Activity...

public class ViewTarget extends ListActivity {
public ListAdapter getListAdapter() {
return super.getListAdapter();
}

public ListView getListView() {
return super.getListView();
}

public void setListAdapter(ListAdapter adapter) {
super.setListAdapter(adapter);
}

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.locations);
Log.v("db", "Inside ViewTarget");
}
}

最佳答案

您在哪个 Android 版本上运行?您可能想尝试改用 NotificationCompat。此类包含在最新的支持包中。

Intent notificationIntent = new Intent(context, ViewTarget.class);
PendingIntent contentIntent = PendingIntent.getActivity(context,
0, notificationIntent,
PendingIntent.FLAG_CANCEL_CURRENT);

NotificationManager nm = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);

Resources res = context.getResources();
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.app_icon)
.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.app_icon))
.setTicker(payload)
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentTitle("Message")
.setContentText(payload);
Notification n = builder.getNotification();

n.defaults |= Notification.DEFAULT_ALL;
nm.notify(0, n);

编辑:我知道这是一个旧线程/问题,但这个答案帮助我在点击通知时显示 Activity 。对于那些这不起作用的人可能是因为您没有在 list 中“注册”该 Activity 。例如:

<activity
android:name="com.package.name.NameOfActivityToLaunch"
android:label="Title of Activity" >
<intent-filter>
<action android:name="com.package.name.NAMEOFACTIVITYTOLAUNCH" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

而且,希望这应该有效。希望对您有所帮助...

关于java - 用户单击通知时如何启动 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10184351/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com