广播接收器上的 onReceive 未在服务中调用
预先感谢您的所有帮助,并对我的英语感到抱歉,这是我的情况,我有一个广播接收器,它以这种方式在启动完成时启动:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BootReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent();
serviceIntent.setAction("com.mypack.service.RicordaPrenotazione");
context.startService(serviceIntent);
}
}
<receiver android:name=".service.BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
此广播接收器在启动完成时启动此服务:
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import org.json.JSONException;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class RicordaPrenotazione extends Service {
public static final String PREFERENCES = "Pref";
public static final String PREF_CODPAZIENTE = "codice paziente";
public static final String PREF_NOME = "nome";
public static final String PREF_COGNOME = "cognome";
public static final String PREF_CF = "codice fiscale";
public static final String PREF_TS = "tessera sanitaria";
public static final String PREF_DATA = "data scadenza";
private final static String LOG_TAG = "LocalService";
private static final int NOTIFICATION = 1;
private NotificationManager notificationManager;
private Notification notification;
private PendingIntent pIntent;
private AlarmManager alarmManager;
private Calendar calendar;
private int notificationNumber;
String codPaziente, nome, cognome, codiceFiscale, tesseraSanitaria, dataScadenza,response;
String[] codicePrenotazione,name, surname, descEsame, descSpecialita, descAmbulatorio, descAvvertenza , dataAppuntamento, oraAppuntamento, statoPrenotazione;
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
WebService webService = new WebService();
int numPrenotazioni;
int count;
@Override
public void onCreate() {
super.onCreate();
SharedPreferences prefs = getSharedPreferences(PREFERENCES,0);
codPaziente = prefs.getString(PREF_CODPAZIENTE, "NESSUN CODICE");
nome = prefs.getString(PREF_NOME, "NESSUN");
cognome = prefs.getString(PREF_COGNOME, "PROFILO");
codiceFiscale = prefs.getString(PREF_CF, "XXXXXX00X00X000X");
tesseraSanitaria = prefs.getString(PREF_TS, "00000000000000000000");
dataScadenza = prefs.getString(PREF_DATA, "GG-MM-AAAA");
count = 0;
try {
mylist = webService.recuperaPrenotazioni(codPaziente.trim());
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(LOG_TAG, "Service Destroyed");
}
@Override
public IBinder onBind(Intent arg0) {
// Ritorno null in quanto non si vuole permettere
// l'accesso al servizio da una applicazione diversa
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startid) {
if(mylist.isEmpty())
{
Toast.makeText(getApplicationContext(),"Nessuna prenotazione trovata!", Toast.LENGTH_LONG).show();
}
else{
numPrenotazioni = mylist.size();
for(int j=0; j<numPrenotazioni; j++){
if(mylist.get(j).get("statoPrenotazione").toString().equals("1")){
count++;
createAlarm(mylist.get(j).get("codicePrenotazione").toString(),mylist.get(j).get("descEsame").toString(),mylist.get(j).get("dataAppuntamento").toString(),mylist.get(j).get("oraAppuntamento").toString(),count);
}
}
}
// Solo per debugging
Log.i(LOG_TAG, "Service Started");
return super.onStartCommand(intent, flags, startid);
}
public void createAlarm (String codicePrenotazioneA, String descEsameA, String dataAppuntamentoA, String oraAppuntamentoA, int count)
{
Calendar cal = Calendar.getInstance();
String[] temp = dataAppuntamentoA.split(" ");
String[] temp2 = oraAppuntamentoA.split(" ");
String[] tempData = temp[0].split("-");
String[] tempOra = temp2[1].split(":");
cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(tempData[2]));
cal.set(Calendar.MONTH, Integer.parseInt(tempData[1]));
cal.set(Calendar.YEAR, Integer.parseInt(tempData[0]));
cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(tempOra[0])-1);
cal.set(Calendar.MINUTE, Integer.parseInt(tempOra[1]));
String ALARM_ACTION = "com.mypack.service.MemoPrenotazione";
Intent alarmintent = new Intent(ALARM_ACTION);
alarmintent.putExtra("codPaziente",codPaziente);
alarmintent.putExtra("nome",nome);
alarmintent.putExtra("cognome",cognome);
alarmintent.putExtra("codicePrenotazione", codicePrenotazioneA);
alarmintent.putExtra("descEsame", descEsameA);
alarmintent.putExtra("dataAppuntamento", dataAppuntamentoA);
alarmintent.putExtra("oraAppuntamento", oraAppuntamentoA);
alarmintent.putExtra("count", count);
PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(), NOTIFICATION,
alarmintent,PendingIntent.FLAG_UPDATE_CURRENT| Intent.FILL_IN_DATA);
AlarmManager am = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
}
}
<service android:name=".service.RicordaPrenotazione">
<intent-filter>
<action android:name="com.mypack.service.RicordaPrenotazione" />
</intent-filter>
</service>
此时一切正常,当启动完成时,广播接收器捕获事件并启动服务,现在的问题是,该服务应该将 AlarmManager 设置为特定时间并启动一个挂起的意图,该意图应该由创建通知的其他广播接收器捕获,这是代码:
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class MemoPrenotazione extends BroadcastReceiver {
public static int NOTIFICATION_ID = 1;
String title, note, codPaziente, nome, cognome;
String codicePrenotazione, descEsame, dataAppuntamento, oraAppuntamento;
int count;
private NotificationManager manger;
private Notification notification;
private PendingIntent contentIntent;
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
codPaziente = extras.getString("codPaziente");
nome = extras.getString("nome");
cognome = extras.getString("cognome");
codicePrenotazione = extras.getString("codicePrenotazione");
descEsame = extras.getString("descEsame");
dataAppuntamento = extras.getString("dataAppuntamento");
oraAppuntamento = extras.getString("oraAppuntamento");
count = extras.getInt("count");
title = "Memo Prenotazione";
note = descEsame + " il " + dataAppuntamento + " alle "
+ oraAppuntamento;
manger = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
int icon = R.drawable.icon;
CharSequence tickerText = "Memo prenotazione";
long when = System.currentTimeMillis();
notification = new Notification(icon, tickerText, when);
Intent i = new Intent(context, HttpGetTaskActivity.class);
Bundle b = new Bundle();
b.putString("codPaziente", codPaziente);
b.putString("nome", nome);
b.putString("cognome", cognome);
i.putExtras(b);
contentIntent = PendingIntent.getActivity(context, NOTIFICATION_ID, i,
PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, title, note, contentIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND; // Suona
notification.defaults |= Notification.DEFAULT_LIGHTS; // LED
notification.defaults |= Notification.DEFAULT_VIBRATE; // Vibra
manger.notify(NOTIFICATION_ID, notification);
}
}
<receiver android:name=".service.MemoPrenotazione">
<intent-filter>
<action android:name="com.mypack.service.MemoPrenotazione" />
</intent-filter>
</receiver>
我认为问题是由于未创建通知,因此未正确调用最后一个广播接收。 有人可以帮助我吗?
Thank you in advance for all your help and sorry for my English, it's my situation, i have a Broadcast Receiver that start at BOOT COMPLETED in this way:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BootReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent();
serviceIntent.setAction("com.mypack.service.RicordaPrenotazione");
context.startService(serviceIntent);
}
}
<receiver android:name=".service.BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
This broadcast receiver when the boot is completed launch this service:
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import org.json.JSONException;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class RicordaPrenotazione extends Service {
public static final String PREFERENCES = "Pref";
public static final String PREF_CODPAZIENTE = "codice paziente";
public static final String PREF_NOME = "nome";
public static final String PREF_COGNOME = "cognome";
public static final String PREF_CF = "codice fiscale";
public static final String PREF_TS = "tessera sanitaria";
public static final String PREF_DATA = "data scadenza";
private final static String LOG_TAG = "LocalService";
private static final int NOTIFICATION = 1;
private NotificationManager notificationManager;
private Notification notification;
private PendingIntent pIntent;
private AlarmManager alarmManager;
private Calendar calendar;
private int notificationNumber;
String codPaziente, nome, cognome, codiceFiscale, tesseraSanitaria, dataScadenza,response;
String[] codicePrenotazione,name, surname, descEsame, descSpecialita, descAmbulatorio, descAvvertenza , dataAppuntamento, oraAppuntamento, statoPrenotazione;
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
WebService webService = new WebService();
int numPrenotazioni;
int count;
@Override
public void onCreate() {
super.onCreate();
SharedPreferences prefs = getSharedPreferences(PREFERENCES,0);
codPaziente = prefs.getString(PREF_CODPAZIENTE, "NESSUN CODICE");
nome = prefs.getString(PREF_NOME, "NESSUN");
cognome = prefs.getString(PREF_COGNOME, "PROFILO");
codiceFiscale = prefs.getString(PREF_CF, "XXXXXX00X00X000X");
tesseraSanitaria = prefs.getString(PREF_TS, "00000000000000000000");
dataScadenza = prefs.getString(PREF_DATA, "GG-MM-AAAA");
count = 0;
try {
mylist = webService.recuperaPrenotazioni(codPaziente.trim());
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(LOG_TAG, "Service Destroyed");
}
@Override
public IBinder onBind(Intent arg0) {
// Ritorno null in quanto non si vuole permettere
// l'accesso al servizio da una applicazione diversa
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startid) {
if(mylist.isEmpty())
{
Toast.makeText(getApplicationContext(),"Nessuna prenotazione trovata!", Toast.LENGTH_LONG).show();
}
else{
numPrenotazioni = mylist.size();
for(int j=0; j<numPrenotazioni; j++){
if(mylist.get(j).get("statoPrenotazione").toString().equals("1")){
count++;
createAlarm(mylist.get(j).get("codicePrenotazione").toString(),mylist.get(j).get("descEsame").toString(),mylist.get(j).get("dataAppuntamento").toString(),mylist.get(j).get("oraAppuntamento").toString(),count);
}
}
}
// Solo per debugging
Log.i(LOG_TAG, "Service Started");
return super.onStartCommand(intent, flags, startid);
}
public void createAlarm (String codicePrenotazioneA, String descEsameA, String dataAppuntamentoA, String oraAppuntamentoA, int count)
{
Calendar cal = Calendar.getInstance();
String[] temp = dataAppuntamentoA.split(" ");
String[] temp2 = oraAppuntamentoA.split(" ");
String[] tempData = temp[0].split("-");
String[] tempOra = temp2[1].split(":");
cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(tempData[2]));
cal.set(Calendar.MONTH, Integer.parseInt(tempData[1]));
cal.set(Calendar.YEAR, Integer.parseInt(tempData[0]));
cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(tempOra[0])-1);
cal.set(Calendar.MINUTE, Integer.parseInt(tempOra[1]));
String ALARM_ACTION = "com.mypack.service.MemoPrenotazione";
Intent alarmintent = new Intent(ALARM_ACTION);
alarmintent.putExtra("codPaziente",codPaziente);
alarmintent.putExtra("nome",nome);
alarmintent.putExtra("cognome",cognome);
alarmintent.putExtra("codicePrenotazione", codicePrenotazioneA);
alarmintent.putExtra("descEsame", descEsameA);
alarmintent.putExtra("dataAppuntamento", dataAppuntamentoA);
alarmintent.putExtra("oraAppuntamento", oraAppuntamentoA);
alarmintent.putExtra("count", count);
PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(), NOTIFICATION,
alarmintent,PendingIntent.FLAG_UPDATE_CURRENT| Intent.FILL_IN_DATA);
AlarmManager am = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
}
}
<service android:name=".service.RicordaPrenotazione">
<intent-filter>
<action android:name="com.mypack.service.RicordaPrenotazione" />
</intent-filter>
</service>
At this time all is ok, when boot is completed the broadcast receiver catch the event and launch the service, and the problem is now, this service should set a AlarmManager to a specific time and launch a pendingintent that should be catch by other broadcast receiver that create a notification, this is the code:
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class MemoPrenotazione extends BroadcastReceiver {
public static int NOTIFICATION_ID = 1;
String title, note, codPaziente, nome, cognome;
String codicePrenotazione, descEsame, dataAppuntamento, oraAppuntamento;
int count;
private NotificationManager manger;
private Notification notification;
private PendingIntent contentIntent;
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
codPaziente = extras.getString("codPaziente");
nome = extras.getString("nome");
cognome = extras.getString("cognome");
codicePrenotazione = extras.getString("codicePrenotazione");
descEsame = extras.getString("descEsame");
dataAppuntamento = extras.getString("dataAppuntamento");
oraAppuntamento = extras.getString("oraAppuntamento");
count = extras.getInt("count");
title = "Memo Prenotazione";
note = descEsame + " il " + dataAppuntamento + " alle "
+ oraAppuntamento;
manger = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
int icon = R.drawable.icon;
CharSequence tickerText = "Memo prenotazione";
long when = System.currentTimeMillis();
notification = new Notification(icon, tickerText, when);
Intent i = new Intent(context, HttpGetTaskActivity.class);
Bundle b = new Bundle();
b.putString("codPaziente", codPaziente);
b.putString("nome", nome);
b.putString("cognome", cognome);
i.putExtras(b);
contentIntent = PendingIntent.getActivity(context, NOTIFICATION_ID, i,
PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, title, note, contentIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND; // Suona
notification.defaults |= Notification.DEFAULT_LIGHTS; // LED
notification.defaults |= Notification.DEFAULT_VIBRATE; // Vibra
manger.notify(NOTIFICATION_ID, notification);
}
}
<receiver android:name=".service.MemoPrenotazione">
<intent-filter>
<action android:name="com.mypack.service.MemoPrenotazione" />
</intent-filter>
</receiver>
I think that the problem is that the last broadcast receive is not being called correctly because the notification is not created.
Someone could help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在 AndroidManifest.xml 中将 MemoPrenotazione 声明为 BroadcastReceiver。
检查此处以查看您需要添加的内容。
编辑:
在 createAlarm 中执行此操作:
这将显式地将广播定向到您的班级。
这也意味着您可以像这样指定清单:
因为您现在不需要过滤器,所以您可以显式地定向广播。
You need to declare MemoPrenotazione as a BroadcastReceiver in your AndroidManifest.xml.
Check here to see what you need to add.
EDIT:
In createAlarm do this instead:
This will explicitly direct the broadcast to your class.
This also means you can specify the manifest like this:
as you don't need the filter now you are explicitly directing the broadcast.