当我单击我的一个片段内的按钮时,我试图设置一个测试警报。但是警报却没有出现。
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>