0

嗨,我在这里问了一个与广播相关的问题

他们建议我在 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);

我的 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);
}

}

现在上次广播接收器在活动处于前台时工作,但这次广播接收器在任何情况下都不工作,谁能告诉我我哪里错了?

4

2 回答 2

0

您希望在前台不运行 Activity 时接收广播。intent-filter为此,您需要receiverAndroidManifest.xml中添加 valid

例如:

<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>
于 2012-12-14T10:13:16.940 回答
0

你没有定义你想要接收什么类型的广播接收器..尝试在清单接收器标签中添加意图过滤器。安卓会发给你。

于 2012-12-14T10:19:24.567 回答