public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "OnReceive alarm test", Toast.LENGTH_SHORT).show();
}
}
//Button is inside a Fragment
Button SetAlarmButton = (Button)(v.findViewById(R.id.SetAlarmButton));
SetAlarmButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
try{
Calendar alarm = Calendar.getInstance();
alarm.set(Calendar.HOUR_OF_DAY, 18);
alarm.set(Calendar.MINUTE, 00);
long alarmMillis = alarm.getTimeInMillis();
Intent intent = new Intent(view.getContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(view.getContext(), 192837, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) view.getContext().getSystemService(view.getContext().ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, alarmMillis , pendingIntent);
}catch(Throwable e){AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
sw.toString();
builder.setMessage(sw.toString());
AlertDialog alert = builder.create();
alert.show();
}
}});
我知道我需要修改我的清单并添加这样的东西,但我不知道如何以及在哪里确切
<receiver android:name="AlarmReceiver">
....
</receiver>
到我的清单
<?xml version="1.0" encoding="UTF-8"?>
<manifest android:versionCode="1" android:versionName="1.0"
package="com.test.alarm" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="15"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application android:icon="@drawable/ic_launcher"
android:label="@string/app_name" android:theme="@style/AppTheme">
<activity android:label="@string/title_activity_main" android:name=".Main">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".First_Start"
/>
<activity
android:name=".Preferences"
android:label="Settings">
</activity>
</application>
</manifest>
答案 0 :(得分:0)
查看AlarmManager的文档:http://developer.android.com/reference/android/app/AlarmManager.html
您正在设置的闹钟与时钟应用程序内置的闹钟功能无关。
系统使用警报来允许应用程序在将来响应某些事件。您基本上告诉系统您希望应用程序在一段时间内被唤醒,并定义在警报响起时调用的Activity。
答案 1 :(得分:0)
Intent i = new Intent(AlarmClock.ACTION_SET_ALARM);
i.putExtra(AlarmClock.EXTRA_MESSAGE, "New Alarm");
i.putExtra(AlarmClock.EXTRA_HOUR, hours);
i.putExtra(AlarmClock.EXTRA_MINUTES, mins);
startActivity(i);
但是我不能指定我想要设置闹钟的那天