您的位置:首页 > 编程语言 > Java开发

BootBroadcastReceiver.java

2015-11-28 11:26 405 查看
与A2dpSinkAvrcpService.java在同一个文件下编译  

 package com.android.a2dpsink;

   import android.content.BroadcastReceiver;  

   import android.content.Context;  

   import android.content.Intent;  

   import android.util.Log;  

   import android.bluetooth.BluetoothA2dpSink;

   import android.bluetooth.BluetoothProfile;     

   import android.bluetooth.BluetoothAdapter;

   public class BootBroadcastReceiver extends BroadcastReceiver {  
private static int NEXT_STATE = 1;
private static final String TAG = "BootBroadcastReceiver";

//蓝牙适配器开启就startservice

  static final String ACTION = "android.bluetooth.adapter.action.STATE_CHANGED";   

  //static final String ACTION = "android.bluetooth.a2dp-sink.profile.action.CONNECTION_STATE_CHANGED";   
//接收这个自定义广播就stopservice

static final String STOP = "BootBroadcastReceiver.javareceiver";

       public void onReceive(Context context, Intent intent) {  
  Intent bIntent = new Intent(context,A2dpSinkAvrcpService.class);
if (intent.getAction().equals(ACTION)){
/* int state = intent.getIntExtra(BluetoothProfile.EXTRA_STATE,NEXT_STATE);
if (1 == state) {
context.startService(bIntent);
           
Log.d(TAG, "startService...");  
}
*/
int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
   if (BluetoothAdapter.STATE_ON == state) {
context.startService(bIntent);
           
Log.d(TAG, "startService...");  
   }
}
if (intent.getAction().equals(STOP)){
context.stopService(bIntent);
Log.d(TAG,"stopService...");
}
}  

  }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息