我在调用我的 BroadcastReceiver 时遇到问题。我不知道问题出在哪里。这是我的代码 Myalarm.class 自动调用 BroadcastReceiver(AlarmReceiver) 。我在广播中做了一些标签,看看他是否打电话……然后我发现广播不会打电话
MyAlarm.class
public class MyAlarm extends SherlockFragmentActivity implements OnClickListener {
TextView textAlarmPrompt;
public static final int NOTIFICATION_ID = 42;
private String notificationTitle;
TimePickerDialog timePickerDialog;
final static int RQS_1 = 1;
String ID_Alerte;
long alarm_GPS;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myalarm);
((Button)this.findViewById(R.id.annuler)).setOnClickListener(this);
Intent i = getIntent();
Bundle myBundle= i.getExtras();
alarm_GPS = myBundle.getLong("alarm_gps");
ID_Alerte = myBundle.getString("id_alerte");
// openTimePickerDialog(false);
///////////// call AlarmReceiver//////////////:
AlarmManager am=(AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
Intent intentlocalisation = new Intent(this,AlarmReceiver.class);
//put data to BroadcastReceiver
intentlocalisation.putExtra("id_alerte", ID_Alerte);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, intentlocalisation, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+3000, alarm_GPS , pi);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.annuler:
///////// Alarm SMS cancel (AlarmReceiver) //////////
Intent intent_stopsms = new Intent(this, AlarmReceiver.class);
PendingIntent sendersms = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent_stopsms, 0);
AlarmManager alarmManagersms = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
alarmManagersms.cancel(sendersms);
Toast.makeText(this, "Cancelled alarm sms", Toast.LENGTH_SHORT).show();
Intent adv=new Intent(MyAlarm.this,
Home.class);
startActivity(adv);
finish();
break;
}
}
}
这是我的警报接收器:
public class AlarmReceiver extends BroadcastReceiver {
String ID_Alerte;
@Override
public void onReceive(Context arg0, Intent arg1) {
arg0.stopService(new Intent(arg0, MyService.class));
///////////////////////////
Log.i("Alarm Receiver","My receive is shown up");
ID_Alerte=arg1.getStringExtra("id_alerte");
Toast.makeText(arg0, "Alarm received!id_alerte=" +ID_Alerte , Toast.LENGTH_LONG).show();
//////////////////////////////////
Intent serviceIntent = new Intent(arg0,MyService.class);
serviceIntent.putExtra("id_alerte", ID_Alerte);
arg0.startService(serviceIntent);
}
}
这是我的 Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.my_app"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="false" />
<application
android:allowBackup="true"
android:icon="@drawable/icon_logo"
android:label="@string/app_name"
android:theme="@style/Theme.SherlockCustom" >
<activity
android:name="com.example.my_app.MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MonProfile" />
<activity android:name=".Register" />
<activity android:name=".Home" />
<activity android:name=".Home1" />
<activity android:name=".Activity3" />
<activity android:name=".MyAlarm" />
<activity android:name=".JSONParsingAlertes" />
<activity android:name=".JSONParsingProches" />
<activity android:name=".Profil_Proche" />
<activity android:name=".Nouveau_Proche" />
<activity android:name=".Modifier_Proche" />
<activity android:name=".detailAlert" />
<activity android:name=".About" />
<activity android:name=".Code_Request" />
<activity
android:name=".Authentification"
android:screenOrientation="portrait" />
<activity
android:name=".Advanced"
android:screenOrientation="portrait" />
<activity
android:name=".Tracer"
android:screenOrientation="portrait" />
<activity android:name=".LegalNoticesActivity" />
<activity
android:name=".ActivityNotification"
android:label="@string/app_name" />
<service android:name=".NotificationService" />
<service android:name=".GetProche" />
<service android:name=".MyService" >
</service>
<receiver
android:name="com.example.my_app.AlarmReceiver"
android:exported="true" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="*************************" />
</application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="com.example.my_app.permission.MAPS_RECEIVE" />
</manifest>
PS:如果我的英语不好,请见谅