我之前问了一个与广播有关的问题here
他们建议我在Manifest文件中添加广播接收器类,然后在Activity的onCreate方法中注册它们的实例 所以这是我的清单文件的代码
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.riteshmapsapplicationdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:icon="@drawable/app_icon"
android:label="Tring On Train"
android:theme="@style/AppTheme" >
<uses-library android:required="true"
android:name="com.google.android.maps"/>
<activity
android:name=".MainActivity"
android:label="Tring On Train" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".AlarmActivity"
android:label="Alarm" />
<activity
android:name=".Address_List_Activity"
android:label="Saved Locations" />
<receiver android:name=".ProximityAlertReceiver" >
</receiver>
</application>
</manifest>
这里是onCreate方法的代码段,我正在注册BroadCast Receiver类的实例。
String s=System.currentTimeMillis()+"";
PendingIntent proximityIntent = PendingIntent.getBroadcast(getApplicationContext(), i, intent, PendingIntent.FLAG_UPDATE_CURRENT);
locationManager.addProximityAlert(
latitude, // the latitude of the central point of the alert region
longitude, // the longitude of the central point of the alert region
1000, // the radius of the central point of the alert region, in meters
-1, // time for this proximity alert, in milliseconds, or -1 to indicate no expiration
proximityIntent // will be used to generate an Intent to fire when entry to or exit from the alert region is detected
);
IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT+s);
registerReceiver(new ProximityAlertReceiver(), filter);
和My ProximityAlert类的代码是
public class ProximityAlertReceiver extends BroadcastReceiver {
private static final int NOTIFICATION_ID = 1000;
public static final String PREFS_NAME = "MyPrefsFile";
@Override
public void onReceive(Context context, Intent intent) {
String key = LocationManager.KEY_PROXIMITY_ENTERING;
SharedPreferences shared=context.getSharedPreferences(MainActivity.PREFS_NAME,0);
Boolean entering = intent.getBooleanExtra(key, false);
Double longitude=intent.getDoubleExtra("longitude", 0.0);
Double latitude=intent.getDoubleExtra("latitude",0.0);
Intent in = new Intent(context,AlarmActivity.class);
in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
in.putExtra("longitude", longitude);
in.putExtra("latitude", latitude);
Log.i("cont1", shared.getInt("count", 0)+"");
in.putExtra("count",shared.getInt("count", 0));
context.startActivity(in);
}
}
现在最后一次广播接收器工作时活动在前台但是这次广播接收器在任何情况下都无法正常工作可以通知我我错了吗?
答案 0 :(得分:0)
您希望在未在前台运行活动时接收广播。为此,您需要在 AndroidManifest.xml
中向intent-filter
添加有效receiver
例如:
<receiver android:name=".ProximityAlertReceiver" >
<intent-filter>
<action android:name="com.your.app.example_filter_1" />
<action android:name="com.your.app.example_filter_2" />
<!-- etc -->
</intent-filter>
</receiver>
答案 1 :(得分:0)
您未定义要接收的广播接收器类型..在清单接收器标签中添加intent-filter。机器人会发给你。