我正在尝试制作提醒应用。我遇到的问题是我无法使BroadcastReceiver工作。 onReceive()函数不会被调用。
这是我的AddActivity.java:
package tywele.remindme;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import java.util.Calendar;
/**
* Created by Simon on 14.07.2014.
* Diese Klasse baut das Layout und die Funktionalität zum Hinzufügen einer Erinnerung auf.
*/
public class AddActivity extends ActionBarActivity {
private EditText reminderEditText;
private TimePicker reminderTimePicker;
private DatePicker reminderDatePicker;
private PendingIntent pendingIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_reminder_layout);
//Färbt die ActionBar
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#009688")));
reminderEditText = (EditText) findViewById(R.id.editText);
reminderTimePicker = (TimePicker) findViewById(R.id.timePicker);
reminderDatePicker = (DatePicker) findViewById(R.id.datePicker);
reminderTimePicker.setIs24HourView(DateFormat.is24HourFormat(this));
}
/**
* Erstellt eine neue Erinnerung und speichert sie in der Datenbank
* Setzt den Alarm für die Erinnerung und ruft den BroadcastReceiver auf
*/
public void OnReminderSubmitButtonClick(View view) {
DatabaseHandler db = (DatabaseHandler) getIntent().getSerializableExtra("db");
Reminder newReminder = new Reminder(reminderEditText.getText().toString(),
reminderTimePicker.getCurrentHour(), reminderTimePicker.getCurrentMinute(),
reminderDatePicker.getDayOfMonth(), reminderDatePicker.getMonth() + 1, reminderDatePicker.getYear());
db.addReminder(newReminder);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, newReminder.getMonth());
calendar.set(Calendar.DAY_OF_MONTH, newReminder.getDay());
calendar.set(Calendar.YEAR, newReminder.getYear());
calendar.set(Calendar.HOUR_OF_DAY, newReminder.getHour());
calendar.set(Calendar.MINUTE, newReminder.getMinute());
calendar.set(Calendar.SECOND, 0);
Intent callNotification = new Intent(this, CustomReceiver.class);
callNotification.putExtra("reminder", newReminder);
pendingIntent = PendingIntent.getBroadcast(this, 101, callNotification, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
setResult(RESULT_OK);
this.finish();
}
}
这是我的CustomReceiver.java:
package tywele.remindme;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
/**
* Created by Simon on 15.07.2014.
*/
public class CustomReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager;
notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
String notificationTitle = ((Reminder) intent.getSerializableExtra("reminder")).getText();
Notification notification = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.notification_small_icon).setContentTitle(notificationTitle).setContentText(notificationTitle).build();
notificationManager.notify(0, notification);
System.out.println("Notification built");
}
}
我的AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="tywele.remindme">
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<application
tools:replace="android:icon"
android:name="com.orm.SugarApp"
android:allowBackup="true"
android:icon="@drawable/remindme_icon"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Light">
<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>
<meta-data
android:name="DATABASE"
android:value="remind_me.db" />
<meta-data
android:name="VERSION"
android:value="1" />
<activity
android:name=".AddActivity"
android:label="@string/add_reminder_text"
android:theme="@style/Theme.AppCompat.Light"
android:parentActivityName=".MainActivity">
</activity>
<receiver android:name="CustomReceiver" android:enabled="true"/>
</application>
</manifest>
永远不会调用onReceive()方法。请帮我! :(
答案 0 :(得分:0)
您需要为CustomReceiver
指定IntentFilter。
AddActivity:
替换
Intent callNotification = new Intent(this, CustomReceiver.class);
与
Intent callNotification = new Intent("tywele.remindme.ACTION_EVENT_TIME");
然后在你的清单中添加:
<receiver android:name=".CustomReceiver" android:enabled="true">
<intent-filter>
<action android:name="tywele.remindme.ACTION_EVENT_TIME" />
</intent-filter>
</receiver>