我有這樣的服務類:無法調用藍牙廣播接收器方法的服務
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());
}
}
}
};
}
而且在我的活動課,我有這樣的:
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);
}
}
的廣播接收器方法(bluetoothReceiver)在Service類中永遠不會被調用。我不知道爲什麼。如果我在活動中首先有IntentFilter和BroadcastReceiver代碼,那麼它可以工作 - 但不是[單獨的]服務。我很難過。
我的AndroidManifest.xml是:
<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>
我是新來的Android所以我在做什麼可能不是最好的。希望可以有人幫幫我。
你在AndroidManifest.xml中給出藍牙權限? – Lucifer 2012-08-03 05:56:34
是的,我在AndroidManifest.xml中擁有這些權限: –
ChuongPham
2012-08-03 05:59:21
有你的註冊收貨人在清單和@Lucifer說你有權利?即使我不認爲你可以做TextView tvStatus =(TextView)mActivity.findViewById(R.id.tvtatus); 在你的接收器..? – AAnkit 2012-08-03 06:00:11