gpt4 book ai didi

安卓 : Notification navigation

转载 作者:行者123 更新时间:2023-11-29 00:11:41 24 4
gpt4 key购买 nike

我正在用 Android 设计一款游戏。当用户一段时间未玩我的游戏时,游戏会生成通知。

问题: 当用户自动点击通知时,必须触发游戏。你能帮我实现这个目标吗?

public class AndroidLauncher extends AndroidApplication implements ActionResolver {

AlarmManager am;

@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
initialize(new MainGame(this), config);
}

@Override
public void showOrLoadInterstital() {
// TODO Auto-generated method stub

}

@Override
public void sendToGoogleAnalytics(String arg) {
// TODO Auto-generated method stub

}

@Override
public void setNotification(String msg) {
// TODO Auto-generated method stub
am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
setOneTimeAlarm(msg);
}

public void setOneTimeAlarm(String msg) {
Intent intent = new Intent(this, TimeAlarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_ONE_SHOT);
am.set(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + (1 * 1000), pendingIntent);
}

时间闹钟

public class TimeAlarm extends BroadcastReceiver {

NotificationManager nm;

@Override
public void onReceive(Context context, Intent intent) {

int currentapiVersion = android.os.Build.VERSION.SDK_INT;
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);

String heading = "Content Heading";
String message = "Message";

String tickerHeading="Ticker Heading";

if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
Notification n;
n = new Notification(R.drawable.notification_icon_72_72, " API < 11 Msg", 0);
n.setLatestEventInfo(context, heading , message, contentIntent);
n.flags = Notification.FLAG_AUTO_CANCEL;
nm.notify(0, n);
}else{
Resources res = context.getResources();
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.notification_icon_72_72)
.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.notification_icon_114_114))
.setTicker(tickerHeading)
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentTitle( heading)
.setContentText(message);
Notification n = builder.build();
nm.notify(1, n);
}

}
}

enter image description here

最佳答案

假设你已经有一个地方,在一定时间后调用代码,这是应该在里面的:

NotificationManager mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);

Intent i = new Intent(context, ActivityToLaunch.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);


NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.my_icon)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.my_large_icon))
.setAutoCancel(true)
.setContentTitle("notification title")
.setStyle(new NotificationCompat.BigTextStyle().bigText("message of notification"))
.setDefaults(Notification.DEFAULT_SOUND)
.setTicker("message of notificaiton in status bar")
.setContentText("message of notificaiton");

mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

关于安卓 : Notification navigation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29751105/

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