Android:AlarmManager 工作不正常。无法设置闹钟
Android : AlarmManager is not working properly. It is not able to set the alarm
这是我设置闹钟的代码。这是我试过的代码,但不幸的是它根本不会设置警报。请帮我解决一下这个。
AlarmManager 是我知道设置警报的唯一方法。谁能告诉我是否还有其他方法。我也在清单中提供了所有必要的权限。
public class NextAlarm extends Activity {
DatePicker pickerDate;
TimePicker pickerTime;
Button buttonSetAlarm;
TextView info;
final static int RQS_1 = 1;
Method to get the date picker and time picker
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next_alarm);
info = (TextView)findViewById(R.id.info);
pickerDate = (DatePicker)findViewById(R.id.pickerdate);
pickerTime = (TimePicker)findViewById(R.id.pickertime);
Calendar now = Calendar.getInstance();
pickerDate.init(
now.get(Calendar.YEAR),
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH),
null);
pickerTime.setCurrentHour(now.get(Calendar.HOUR_OF_DAY));
pickerTime.setCurrentMinute(now.get(Calendar.MINUTE));
buttonSetAlarm = (Button)findViewById(R.id.setalarm);
buttonSetAlarm.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0) {
Calendar current = Calendar.getInstance();
Calendar cal = Calendar.getInstance();
cal.set(pickerDate.getYear(),
pickerDate.getMonth(),
pickerDate.getDayOfMonth(),
pickerTime.getCurrentHour(),
pickerTime.getCurrentMinute(),
00);
if(cal.compareTo(current) <= 0){
//The set Date/Time already passed
Toast.makeText(getApplicationContext(),
"Invalid Date/Time",
Toast.LENGTH_LONG).show();
}else{
setAlarm(cal);
}
}});
}
private void setAlarm(Calendar targetCal){
info.setText("\n\n***\n"
+ "Alarm is set@ " + targetCal.getTime() + "\n"
+ "***\n");
Intent intent = new Intent(NextAlarm.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(NextAlarm.this, RQS_1, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
finish();
}
}
_________________________________________________________________
Receiver Class
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
notificationStatus(arg0);
}
private void notificationStatus(Context context) {
final NotificationManager mNotificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
final int icon = R.drawable.ic_launcher;
final Notification notification = new Notification(icon, "test", System.currentTimeMillis());
final Intent notificationIntent = new Intent(context.getApplicationContext(), NextAlarm.class);
final PendingIntent pIntent = PendingIntent.getActivity(context.getApplicationContext(), 0, notificationIntent, 0);
notification.setLatestEventInfo(context, "ticker", "title", pIntent);
mNotificationManager.notify(1, notification);
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ctsprojects.com.alarmapp" >
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".songList"
android:label="@string/title_activity_song_list" >
</activity>
<activity
android:name=".NextAlarm"
android:label="@string/title_activity_next_alarm" >
</activity>
<receiver android:name=".AlarmReceiver" android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"></action>
</intent-filter>
</receiver>
</application>
</manifest>
通过警报管理器设置警报的代码:
AlarmManager alarmManager = (AlarmManager) getBaseContext().getSystemService(ALARM_SERVICE);
Intent i1 = new Intent(this, ReceiveAlarmActivity.class);
i1.putExtra("Key", "Value");
PendingIntent operation = PendingIntent.getActivity(getBaseContext(), 0, i1, PendingIntent.FLAG_ONE_SHOT);
if (Build.VERSION.SDK_INT >= 19){
alarmManager.setExact(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), operation);
}else{
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), operation);
}
完成
这就是我如何能够从 activity 向 Android 的 ALARM 应用程序设置闹钟。使用 AlarmManager 似乎是不可能的。谢谢你的回答。
Intent i = new Intent(AlarmClock.ACTION_SET_ALARM);
l = getIntent().getExtras().getString("song");
i.putExtra(AlarmClock.EXTRA_HOUR, pickerTime.getCurrentHour());
i.putExtra(AlarmClock.EXTRA_MINUTES, pickerTime.getCurrentMinute());
i.putExtra(AlarmClock.EXTRA_RINGTONE, l);
这是我设置闹钟的代码。这是我试过的代码,但不幸的是它根本不会设置警报。请帮我解决一下这个。 AlarmManager 是我知道设置警报的唯一方法。谁能告诉我是否还有其他方法。我也在清单中提供了所有必要的权限。
public class NextAlarm extends Activity {
DatePicker pickerDate;
TimePicker pickerTime;
Button buttonSetAlarm;
TextView info;
final static int RQS_1 = 1;
Method to get the date picker and time picker
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next_alarm);
info = (TextView)findViewById(R.id.info);
pickerDate = (DatePicker)findViewById(R.id.pickerdate);
pickerTime = (TimePicker)findViewById(R.id.pickertime);
Calendar now = Calendar.getInstance();
pickerDate.init(
now.get(Calendar.YEAR),
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH),
null);
pickerTime.setCurrentHour(now.get(Calendar.HOUR_OF_DAY));
pickerTime.setCurrentMinute(now.get(Calendar.MINUTE));
buttonSetAlarm = (Button)findViewById(R.id.setalarm);
buttonSetAlarm.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0) {
Calendar current = Calendar.getInstance();
Calendar cal = Calendar.getInstance();
cal.set(pickerDate.getYear(),
pickerDate.getMonth(),
pickerDate.getDayOfMonth(),
pickerTime.getCurrentHour(),
pickerTime.getCurrentMinute(),
00);
if(cal.compareTo(current) <= 0){
//The set Date/Time already passed
Toast.makeText(getApplicationContext(),
"Invalid Date/Time",
Toast.LENGTH_LONG).show();
}else{
setAlarm(cal);
}
}});
}
private void setAlarm(Calendar targetCal){
info.setText("\n\n***\n"
+ "Alarm is set@ " + targetCal.getTime() + "\n"
+ "***\n");
Intent intent = new Intent(NextAlarm.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(NextAlarm.this, RQS_1, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
finish();
}
}
_________________________________________________________________
Receiver Class
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
notificationStatus(arg0);
}
private void notificationStatus(Context context) {
final NotificationManager mNotificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
final int icon = R.drawable.ic_launcher;
final Notification notification = new Notification(icon, "test", System.currentTimeMillis());
final Intent notificationIntent = new Intent(context.getApplicationContext(), NextAlarm.class);
final PendingIntent pIntent = PendingIntent.getActivity(context.getApplicationContext(), 0, notificationIntent, 0);
notification.setLatestEventInfo(context, "ticker", "title", pIntent);
mNotificationManager.notify(1, notification);
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ctsprojects.com.alarmapp" >
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".songList"
android:label="@string/title_activity_song_list" >
</activity>
<activity
android:name=".NextAlarm"
android:label="@string/title_activity_next_alarm" >
</activity>
<receiver android:name=".AlarmReceiver" android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"></action>
</intent-filter>
</receiver>
</application>
</manifest>
通过警报管理器设置警报的代码:
AlarmManager alarmManager = (AlarmManager) getBaseContext().getSystemService(ALARM_SERVICE);
Intent i1 = new Intent(this, ReceiveAlarmActivity.class);
i1.putExtra("Key", "Value");
PendingIntent operation = PendingIntent.getActivity(getBaseContext(), 0, i1, PendingIntent.FLAG_ONE_SHOT);
if (Build.VERSION.SDK_INT >= 19){
alarmManager.setExact(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), operation);
}else{
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), operation);
}
完成
这就是我如何能够从 activity 向 Android 的 ALARM 应用程序设置闹钟。使用 AlarmManager 似乎是不可能的。谢谢你的回答。
Intent i = new Intent(AlarmClock.ACTION_SET_ALARM);
l = getIntent().getExtras().getString("song");
i.putExtra(AlarmClock.EXTRA_HOUR, pickerTime.getCurrentHour());
i.putExtra(AlarmClock.EXTRA_MINUTES, pickerTime.getCurrentMinute());
i.putExtra(AlarmClock.EXTRA_RINGTONE, l);