2017-06-02 24 views
0

目标防止从产卵时,蓝牙设备连接

  • 如果蓝牙设备连接,无活动正在运行一个新的活动,开始活动
  • 如果蓝牙设备连接,和活动是已在运行,连接到已经运行活动

问题

  • 一旦设备连接,一个新的活动启动。我无法使应用重复使用相同的活动。

我已经设法解决

  • 如果蓝牙设备连接,无活动正在运行时,启动活动

的问题表现在使用BroadCastReceivers的这依次使用意图启动活动。出于某种原因,活动会在其生命周期中继续运行,在新设备连接时产生新窗口。

我已经在Nexus 6P上用Android N测试过了。我不知道这个实现对于其他任何设备意味着什么样的含义。但我至少需要得到这个工作一个设备。

舱单

<uses-permission android:name="android.permission.BLUETOOTH" /> 
<uses-permission android:name="android.permission.VIBRATE" /> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".MainActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity android:name=".BtActivity" /> 
    <receiver android:name=".BtConnectionBroadcastReceiver" android:priority="100000"> 
     <intent-filter> 
      <action android:name="android.bluetooth.device.action.ACL_CONNECTED" /> 
      <action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" /> 
      <action android:name="android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED" /> 
      <action android:name="android.intent.action.MEDIA_BUTTON" /> 
      <action android:name="android.media.VOLUME_CHANGED_ACTION" /> 
     </intent-filter> 
    </receiver> 
</application> 

BtConnectionBroadcastReceiver

public class BtConnectionBroadcastReceiver extends BroadcastReceiver { 
    private static final String TAG = "BT"; 
    public static final String BROADCAST_ACTION_CONNECTED = "CONNECTED"; 
    public static final String BROADCAST_ACTION_DISCONNECTED = "DISCONNECTED"; 
    SharedPreferences mSharedPreferences; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     String action = intent.getAction(); 

     // When discovery finds a device 
     if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) { 
      // Get the BluetoothDevice object from the Intent 
      Log.d(TAG, "DEVICE CONNECTED"); 
      BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
      Log.d("DEVICE NAME", device.getName()); 
      Log.d("DEVICE ADDRESS", device.getAddress()); 
      Intent i = new Intent(context, BtActivity.class); 
      context.startActivity(i); 
     } else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) { 
      Log.d(TAG, "DEVICE DISCONNECTED"); 
      intent = new Intent(); 
      intent.setAction(BtConnectionBroadcastReceiver.BROADCAST_ACTION_DISCONNECTED); 
      context.sendBroadcast(intent); 
     } 
    } 

BtActivity

public class BtActivity extends AppCompatActivity { 
private static final String TAG = "BT"; 
Window mWindow; 

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

    setContentView(R.layout.activity_bt); 

    Log.d(TAG, "onCreate"); 
    IntentFilter filter = new IntentFilter(BtConnectionBroadcastReceiver.INTENT_FILTER); 
    filter.addAction(BtConnectionBroadcastReceiver.BROADCAST_ACTION_CONNECTED); 
    filter.addAction(BtConnectionBroadcastReceiver.BROADCAST_ACTION_DISCONNECTED); 
    //registerReceiver(mReceiver, filter); 

    mWindow = getWindow(); 
    WindowManager.LayoutParams params = new WindowManager.LayoutParams(); 
    //params.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF; 
    params.screenBrightness = 0.2f; 
    mWindow.setAttributes(params); 
    mWindow.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); 
    mWindow.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); 
    mWindow.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); 
    mWindow.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
    mWindow.addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE); 
    mWindow.getDecorView().setSystemUiVisibility(
      View.SYSTEM_UI_FLAG_LAYOUT_STABLE | 
        View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | 
        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | 
        View.SYSTEM_UI_FLAG_FULLSCREEN | 
        View.SYSTEM_UI_FLAG_IMMERSIVE); 
    } 

@Override 
protected void onResume() { 
    super.onResume(); 
    Log.d(TAG, "onResume"); 
} 

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    Log.d(TAG, "onDestroy"); 
} 
} 

当我运行此代码,我得到以下链:

  1. 开始MainActivity(不包括在内,它仅包含默认的主布局的活动,从而使应用程序接收器注册)
  2. 开关蓝牙设备上(这在前面已经配对,因此Android知道这事)
  3. 等待它连接,并得到这个:
    • 设备相连
    • 的onCreate
    • 的onResume

我不能把握,为什么活性是在这一点上重新启动。该活动已在运行,BroadcastReceiver仅向正在运行的活动发送广播。我无法弄清楚为什么有一个活动自杀的原因,然后再次重新启动。

+1

当您启动活动时,尝试向您的意图添加Intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)。正如文档所述:如果已设置,则活动将不会启动,前提是它已经在历史堆栈的顶部运行。希望这是你正在寻找的! –

+0

这个问题需要分解为点。我在这里添加了另一个有关蓝牙连接状态与broadcastreceivers的问题:https://stackoverflow.com/questions/44367910/prevent-ondestroy-when-bluetooth-connection-state-changes –

回答

0

尝试将启动模式设置为正在启动的活动。

android:launchMode="singleTop" 

这也提供意图相同的活动来说,如果这个活动是目前在该任务和活动的onNewIntent()()的方法最顶层的活动将被调用,而不是中的onCreate。并通过传递意图额外功能来管理功能。如果此活动不是其任务中最活跃的活动,则将创建新活动实例,并调用onCreate()后跟onResume()。

其他启动模式,如“singleTask”/“singleInstance”也可以根据需要使用。

希望这会有所帮助。

+0

这有助于不产生新的活动,但是,它在蓝牙连接状态改变时不会帮助防止onDestroy被调用。我会接受答案并发布另一个答案,因为我意识到这个问题包含两个问题。我也会编辑这个问题,所以它很重要。 –