0

java文件---Bttest11.java

**package niebttest11.example.bluetoothtesta;
/*import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class Bttest11 extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bttest11);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.bttest11, menu);
        return true;
    }
    */
import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class Bttest11 extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {  

        int state = intent.getExtras().getInt(BluetoothAdapter.EXTRA_STATE);

     switch (state) {

   case BluetoothAdapter.STATE_OFF:

      Toast.makeText(context, "Off", Toast.LENGTH_SHORT).show();

           break;

   case BluetoothAdapter.STATE_TURNING_OFF:

   Toast.makeText(context, "Turning Off", Toast.LENGTH_SHORT).show();

        break;

     case BluetoothAdapter.STATE_ON:
            Toast.makeText(context, "On", Toast.LENGTH_SHORT).show();

          break;

 case BluetoothAdapter.STATE_TURNING_ON:

      Toast.makeText(context, "Turning On", Toast.LENGTH_SHORT).show();

         break;
        }
    }
}**



//manifest file---Bluetoothtest11 Manifest


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="niebttest11.example.bluetoothtesta"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="8" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <uses-permission android:name="android.permission.BLUETOOTH"/>


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="niebttest11.example.bluetoothtesta.Bttest11"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <receiver android:name="Bttest11" >
            <intent-filter>
            <action android:name="android.bluetooth.adapter.action.ACTION_FOUND"/>
            <action android:name="android.bluetooth.adapter.action.ACTION_DISCOVERY_FINISHED"/>

            </intent-filter>
        </receiver>
    </application>

</manifest>

错误:

代码正在运行。但是在 android 手机中运行 .apk 时,它会显示一个错误,

“应用程序 Bluetoothtest11(进程 nietest11.example.buetoothtesta)已意外停止”。

4

1 回答 1

0

如果您仅使用 BroadcastReceiver 则不要在活动 bcoz 中写 Bttest11 那不是活动。

如果你想要活动,那么创建一个活动类并从那里调用接收器。在 android manifest 中定义该活动。在活动标签下,您的接收者在接收者标签中写入

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    startService(new Intent(MainActivity.this, MyService.class));
}

}

于 2013-03-09T10:33:15.360 回答