我正在我的应用程序中使用基于警报的应用程序我已经使用以下代码设置了警报。如果我创建警报意味着它的创建,但警报不会移动到广播接收器。我已经尝试了几次,但它对我不起作用。即使我已经在 android manifest 文件中正确添加了广播接收器。
1.设置报警类:
public class Alertedit extends Activity {
boolean AlarmEnabled;
final static int RQS_1 = 1;
TimePicker myTimePicker;
private SQLiteConnectornew loginDataBaseAdapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alertaddlist);
Button bt1 = (Button) findViewById(R.id.alerttime);
bt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
openTimePickerDialog(false);
}});}
private void openTimePickerDialog(boolean is24r){
Calendar calendar = Calendar.getInstance();
timePickerDialog = new TimePickerDialog(
Alertedit.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) {
// TODO Auto-generated method stub
bt1.setText("" + targetCal.getTime());
Log.d("set", "Alarm set");
Intent intent = new Intent(Alertedit.this, AlarmReciever.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(Alertedit.this, RQS_1, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
}};
2.接收类:
public class AlarmReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
try {
/* Display an alert */
Log.e("value", "how are you??");
Toast.makeText(context, "How are you?", Toast.LENGTH_LONG).show();
}
catch (Exception r) {
Toast.makeText(context, "You were supposed to do something"
+" now but I can't retrieve what it was.",
Toast.LENGTH_SHORT).show();
Log.e("ALARM_RECEIVER", r.toString());
}
}
}
3.清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.contactlist"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.contactlist.Primemfrontpage"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<activity
android:name="com.example.contactlist.Login"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.example.contactlist.Registration"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.example.contactlistnew.Alertedit"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent.NoTitleBar" >
</activity>
<receiver android:name="com.example.contactlistnew.AlarmReceiver">
</receiver></application></manifest>