不能在一个服务调用蓝牙的BroadcastReceiver方法 [英] Can't call a Bluetooth BroadcastReceiver method in a Service

查看:368
本文介绍了不能在一个服务调用蓝牙的BroadcastReceiver方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的服务类:

 public class BluetoothService extends Service {

    private static Activity mActivity;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        this.registerReceiver(bluetoothReceiver, intentFilter);     
    }

    @Override
    public void onDestroy() {
        if (bluetoothReceiver != null) {
            this.unregisterReceiver(bluetoothReceiver);
        }       
    }

    @Override
    public void onStart(Intent intent, int startid) {
           //
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {        
        return START_STICKY;
    }

    public static BroadcastReceiver bluetoothReceiver = new BroadcastReceiver(){
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
                final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
                TextView tvStatus = (TextView) mActivity.findViewById(R.id.tvtatus);
                Messaging.appendMessage(tvStatus, Bluetooth.getDeviceState(state));
                if (Bluetooth.isBluetoothEnabled()) {
                    Messaging.appendMessage(tvStatus, Bluetooth.showMessage());
                }               
            }
        }
    };  
}

和我的活动课,我有这样的:

And in my Activity class, I have this:

public class MainActivity extends Activity {

    private TextView tvStatus;
    private Intent intentBluetooth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

       tvStatus = (TextView)findViewById(R.id.tvtatus);

       intentBluetooth = new Intent(this, BluetoothService.class);
       startService(intentBluetooth);
    }
}

在服务类中的BroadcastReceiver的方法(bluetoothReceiver)不会被调用。我不知道为什么。如果我有IntentFilter的和的BroadcastReceiver codeS最重要的一个活动,那么它的作品 - 但不是在一个[单独]服务。我很为难。

The BroadcastReceiver method (bluetoothReceiver) in the Service class is never called. I don't know why. If I have the IntentFilter and the BroadcastReceiver codes above all in an Activity, then it works - but not in a [separate] Service. I'm stumped.

我的Andr​​oidManifest.xml是:

My AndroidManifest.xml is:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.onegoal.androidexample"
        android:versionCode="1"
        android:versionName="1.0.0" 
        android:installLocation="auto"
        android:hardwareAccelerated="true">

        <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="15" />

        <uses-permission android:name="android.permission.BLUETOOTH" />
        <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
        <uses-feature android:name="android.hardware.bluetooth" android:required="false" /> 

        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" 
            android:debuggable="true" >
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name"
                android:theme="@android:style/Theme.NoTitleBar" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <service android:name=".BluetoothService">
            </service>
        </application>  
    </manifest>

我是新来的Andr​​oid所以我在做什么可能不是最好的。希望有人能帮助我。

I'm new to Android so what I'm doing may not be the best. Hope someone can help me.

推荐答案

也许这其实你的接收机是静态引起的问题。

的BroadcastReceiver 不应该是一成不变的。它可能会导致很多问题。

BroadcastReceiver should never be static. it can cause lots of problems.

等非常糟糕的设计问题,您的code - 持有引用内部服务活动,并用它来修改意见真是错误的做法。它可以很容易造成内存韭菜。 右为什么服务之间的通信活动被执行Android的信使,或通过的BroadcastReceiver 。

other really bad design problem with your code - holding reference to activity inside service, and using it to modify views is really wrong thing to do. it can cause easily to memory leek. the right why to communicate between Service and Activity is by implement android's Messanger, or sending broadcasts between them via BroadcastReceiver.

如果你听我的劝告 - 你不会有让你的接收机静态的(我猜你做了它的静态只是因为你在里面使用mActivity静态实例) 我就是pretty的肯定,这将解决您的问题。

if you'll listen to my advice - you won't be have to make your receiver static (I guess you've made it static only because you are using the mActivity static instance inside) and I'm pretty sure it will solve your problem

您可以阅读信使此处的 http://developer.android.com/reference/android/os/Messenger.html

相信你会发现很多的用法示例的网。

sure you'll find lots of usage examples in the net.

例如广播更新,从服务活动:

example of broadcasting updates to the activity from service:

public class MyService extends Service {

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate() {
    super.onCreate();

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
    this.registerReceiver(bluetoothReceiver, intentFilter);
}

@Override
public void onDestroy() {
    if (bluetoothReceiver != null) {
        this.unregisterReceiver(bluetoothReceiver);
    }
    super.onDestroy();
}

public BroadcastReceiver bluetoothReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
            final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
            updateUIWithNewState(state);
        }
    }
};

protected void updateUIWithNewState(int state) {
    Intent intent = new Intent("serviceUpdateReceivedAction");
    intent.putExtra("state", state);
    sendBroadcast(intent);
}

}

这就是活动:

and that's the activity:

public class MyActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = new Intent(this, MyService.class);
    startService(intent);
}

@Override
protected void onResume() {
    super.onResume();

    registerReceiver(mServiceUpdatesReceiver, new IntentFilter("serviceUpdateReceivedAction"));
}

@Override
protected void onPause() {
    unregisterReceiver(mServiceUpdatesReceiver);
    super.onPause();
}

private BroadcastReceiver mServiceUpdatesReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        int state = intent.getIntExtra("state", -1);
        // do what ever you want in the UI according to the state
    }
};
}

这篇关于不能在一个服务调用蓝牙的BroadcastReceiver方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆