我正在创建每日通知应用程序,允许用户设置时间。要开发此功能,我正在使用AlarmReceiver和BroadcastReceiver。
我想在用户单击通知时在对话框/警报框中显示通知消息。
有人可以帮助我实现此功能吗?
下面是我设置通知消息的代码。
public class AlarmReceiver extends BroadcastReceiver {
String TAG = "AlarmReceiver";
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction() != null && context != null) {
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
// Set the alarm here.
Log.d(TAG, "onReceive: BOOT_COMPLETED");
LocalData localData = new LocalData(context);
NotificationScheduler.setReminder(context, AlarmReceiver.class, localData.get_hour(), localData.get_min());
return;
}
}
Log.d(TAG, "onReceive: ");
//Trigger the notification
NotificationScheduler.showNotification(context, MainActivity.class,
"You have New Notification", "check it now?");
}
}
在单击“通知”之后,输出应为此类...
答案 0 :(得分:0)
打开活动的具有待定意图的通知代码,并且该活动应针对该弹出窗口进行编码
Notification notif = new Notification(R.drawable.ic_launcher,"List of Contacts...",
System.currentTimeMillis());
Intent notificationIntent = new Intent(context,AllContacts.class);
notificationIntent.putExtra("from", "notification")
notificationIntent.putExtra("message", "yourmessage")
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notif.setLatestEventInfo(context, from, message, contentIntent);
nm.notify(1, notif);
以及onCreate中的AllContacts.class
if(getIntent.getStringExtras("from").equals("notification")){
//popup show with message
}
答案 1 :(得分:0)
Intent notifyIntent = new Intent(context,YourActivityClassHere.class);
notifyIntent.setFlag(Intent.FLAG_ACTIVITY_NEW_TASK);
//UNIQUE_ID if you expect more than one notification to appear
PendingIntent intent = PendingIntent.getActivity(SimpleNotification.this, UNIQUE_ID,
notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
只需使PendingIntent打开您的一个活动,并使您的活动完全透明,然后打开一个对话框即可。
编辑:如果要从通知单击事件中打开活动:
假设notif是您的Notification对象:
Intent notificationIntent = new Intent(this.getApplicationContext(), ActivityToStart.class);
PendingIntent contentIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, notificationIntent, 0);
notif.contentIntent = contentIntent;
更多detail
答案 2 :(得分:0)
如下更改您的AlarmReceiver
类
public class AlarmReceiver extends BroadcastReceiver {
String TAG = "AlarmReceiver";
public static String NotificationMsg="You have 5 unwatched videos", NotificationTitle = "Watch them now?";
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction() != null && context != null) {
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
// Set the alarm here.
Log.d(TAG, "onReceive: BOOT_COMPLETED");
LocalData localData = new LocalData(context);
NotificationScheduler.setReminder(context, AlarmReceiver.class, localData.get_hour(), localData.get_min());
return;
}
}
Log.d(TAG, "onReceive: ");
//Trigger the notification
NotificationScheduler.showNotification(context, NotificationActivity.class,
NotificationTitle, NotificationMsg);
}
}
然后在此处创建新活动我创建了NotificationActivity
public class NotificationActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ShowDialog();
}
@SuppressLint("ResourceAsColor")
public void ShowDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(NotificationActivity.this);
builder.setMessage("\n" + NotificationMsg);
TextView title = new TextView(NotificationActivity.this);
title.setText(NotificationTitle);
title.setBackgroundColor(Color.DKGRAY);
title.setPadding(20, 20, 20, 20);
title.setGravity(Gravity.CENTER);
title.setTextColor(Color.WHITE);
title.setTextSize(20);
builder.setCustomTitle(title)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//finishAffinity();
}
});
builder.show();
}
}
输出在这里-
希望这会起作用... 请不要忘记接受答案和投票...