Android 关于警报通知的问题
Hiii 我正在尝试使用广播接收器和报警管理器在android中开发一个报警通知。我曾尝试开发代码,在其中我可以设置报警,但问题是设置报警后,我应该得到一些提醒通知。但它不起作用…请提些建议 这是密码 Alarm.javaAndroid 关于警报通知的问题,android,android-layout,android-intent,android-activity,android-fragments,Android,Android Layout,Android Intent,Android Activity,Android Fragments,Hiii 我正在尝试使用广播接收器和报警管理器在android中开发一个报警通知。我曾尝试开发代码,在其中我可以设置报警,但问题是设置报警后,我应该得到一些提醒通知。但它不起作用…请提些建议 这是密码 Alarm.java public class Alarm extends ActionBarActivity { TimePicker myTimePicker; Button buttonstartSetDialog; TextView textAlarmProm
public class Alarm extends ActionBarActivity
{
TimePicker myTimePicker;
Button buttonstartSetDialog;
TextView textAlarmPrompt;
TimePickerDialog timePickerDialog;
final static int RQS_1 = 1;
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alarm);
textAlarmPrompt=(TextView)findViewById(R.id.alarm);
buttonstartSetDialog = (Button)findViewById(R.id.startSetDialog);
buttonstartSetDialog.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
textAlarmPrompt.setText("");
openTimePickerDialog(false);
}
});
}
private void openTimePickerDialog(boolean is24r){
Calendar calendar = Calendar.getInstance();
timePickerDialog = new TimePickerDialog(
Alarm.this,
onTimeSetListener,
calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE),
is24r);
timePickerDialog.setTitle("Set Alarm Time");
timePickerDialog.show();
}
OnTimeSetListener onTimeSetListener
= new OnTimeSetListener(){
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Calendar calNow = Calendar.getInstance();
Calendar calSet = (Calendar) calNow.clone();
calSet.set(Calendar.HOUR_OF_DAY, hourOfDay);
calSet.set(Calendar.MINUTE, minute);
calSet.set(Calendar.SECOND, 0);
calSet.set(Calendar.MILLISECOND, 0);
if(calSet.compareTo(calNow) <= 0){
//Today Set time passed, count to tomorrow
calSet.add(Calendar.DATE, 1);
}
setAlarm(calSet);
}};
private void setAlarm(Calendar targetCal){
textAlarmPrompt.setText(
"\n\n***\n"
+ "Alarm is set@ " + targetCal.getTime() + "\n"
+ "***\n");
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
}
}
ManifestFile.java
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.notificationalarm"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.notificationalarm.Alarm"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".AlarmReceiver" android:process=":remote" />
</application>
</manifest>
请帮助…因为我在您上面的代码中发现,广播接收器中没有用于创建通知的通知代码。我建议您使用此。。。有关在receiver中创建通知的参考信息,请参阅此代码
public class AlarmReceiver extends BroadcastReceiver {
private final String SOMEACTION = "com.manish.alarm.ACTION";
@Override
public void onReceive(Context context, Intent intent) {
generateNotification(context,"Hi how are you?");
String action = intent.getAction();
if (SOMEACTION.equals(action)) {
//do what you want here
generateNotification(context,"Hi how are you?");
}
}
@SuppressWarnings("deprecation")
private void generateNotification(Context context, String message) {
System.out.println(message+"++++++++++2");
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
String subTitle = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, OutPut.class);
notificationIntent.putExtra("content", message);
PendingIntent intent = PendingIntent.getActivity(context, 0,notificationIntent, 0);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
notification.setLatestEventInfo(context, title, subTitle, intent);
//To play the default sound with your notification:
notification.defaults |= Notification.DEFAULT_SOUND;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
}检查一下,对不起,我的朋友。我没有否决你的回答。我只是加了一句评论。这是社区中的另一个人。
public class AlarmReceiver extends BroadcastReceiver {
private final String SOMEACTION = "com.manish.alarm.ACTION";
@Override
public void onReceive(Context context, Intent intent) {
generateNotification(context,"Hi how are you?");
String action = intent.getAction();
if (SOMEACTION.equals(action)) {
//do what you want here
generateNotification(context,"Hi how are you?");
}
}
@SuppressWarnings("deprecation")
private void generateNotification(Context context, String message) {
System.out.println(message+"++++++++++2");
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
String subTitle = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, OutPut.class);
notificationIntent.putExtra("content", message);
PendingIntent intent = PendingIntent.getActivity(context, 0,notificationIntent, 0);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
notification.setLatestEventInfo(context, title, subTitle, intent);
//To play the default sound with your notification:
notification.defaults |= Notification.DEFAULT_SOUND;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}