Android无法将变量从广播接收器传递到其他类

时间:2012-07-19 05:51:15

标签: android service broadcastreceiver

嘿,我正在研究需要将广播接收者的变量接收传递给服务类

的应用

我看过这篇文章 How to pass value from Broadcast receiver to Activity in android? 但它不起作用。

这是我的代码

public class BatteryReceiver extends BroadcastReceiver {
int batteryLevel;
int batteryScale;
private int status;
private boolean isCharging;
@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Log.v("test battery", "battery:" + batteryLevel);
    batteryLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
    batteryScale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 0);

    //Log.v("charging state", "state:" + isCharging);
    status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
    isCharging = status == BatteryManager.BATTERY_STATUS_FULL||status == BatteryManager.BATTERY_STATUS_CHARGING;

    Intent service = new Intent(context, MyService.class);      
    service.putExtra("BatteryLevel", batteryLevel);
    service.putExtra("BatteryScale", batteryScale);
    service.putExtra("Charging", isCharging);
context.startService(service);
}

}

接收3变量的服务类

public class MyService extends Service {
int batteryLevel;
int batteryScale;
private boolean isCharging;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    batteryLevel = intent.getIntExtra("BatteryLevel", batteryLevel);
    batteryScale = intent.getIntExtra("BatteryScale", batteryScale);
    isCharging = intent.getBooleanExtra("Charging", isCharging);

    Log.v("test battery", "battery:" + batteryLevel);
    Log.v("charging state", "state:" + isCharging);

           //other method

           return Service.START_STICKY;
}

最后,日志总是显示batterylevel为0且充电状态为false,这意味着varibale没有传递给sercive

当人们引导我时,我已将值赋给变量,如上所示,但这仍然不起作用

这是我的主要节日,我想这里可能有问题                                            

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".MyApp">
    </activity>

    <service 
        android:name=".MyService"
        android:enabled="true">
    </service>

    <receiver 
        android:name=".BatteryReceiver">
        <intent-filter>
             <action android:name="android.intent.action.BATTERY_CHANGED"></action>
        </intent-filter>
    </receiver>

</application>

在接收器部分,eclispe返回“导出的接收器不需要许可”

在我的应用程序中,服务将继续在backgroud中运行并尝试从braodcastreceiver接收变量以执行该功能

我不确定哪个部分我做错了,如果你发现错误请告诉我,非常感谢你。

1 个答案:

答案 0 :(得分:1)

你应该做类似下面的事情,你怎么能期望变量在不加入变量的情况下具有价值

batteryLevel   =    intent.getIntExtra("BatteryLevel", batteryLevel);
 batteryScale  =       intent.getIntExtra("BatteryScale", batteryScale);
   isCharging   =       intent.getBooleanExtra("Charging", isCharging);

以及Receiver呼叫服务的地方。

你应该致电startService(intent); //这个意图应该是你分配/设定的值。

intent.putExtra("BatteryLevel", batteryLevel);
intent.putExtra("BatteryScale", batteryScale);
intent.putExtra("Charging", isCharging);

与上面的代码一样,您将值设置为intent但未将此intent传递给service,调用startService(intent);并在服务中使用onstart()方法中的这些值

接收器中的

你将服务名称设置为WifiService.class,你的服务名称是MyService,它是如何实现的。将其命名为