连接蓝牙设备时启动应用程序
Start application when bluetooth device is connected
当某些蓝牙设备已连接时,如何在我的 android phone 中启动应用程序?
我正在尝试使用广播接收器执行此操作,但我遇到一个问题...当我完成 activity(退出应用程序)时,广播接收器必须未注册...所以没有收听连接设备的方式...
如何在应用程序完成后让这个广播接收器保持活动状态?
谢谢
编辑:
public class AutostartReceiver extends BroadcastReceiver
{
/**
* @param context Context
* @param intent Intent
* @return void
*/
@Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
switch (action)
{
case BluetoothDevice.ACTION_ACL_CONNECTED:
Log.v("PAREK", "cnn");
break;
case BluetoothDevice.ACTION_ACL_DISCONNECTED:
Log.v("PAREK", "dnn");
break;
}
}
}
我也尝试了清单初始化
<receiver
android:name=".Model.Tools.AutostartReceiver">
<intent-filter>
<action android:name="android.bluetooth.BluetoothDevice.ACTION_ACL_CONNECTED" />
<action android:name="android.bluetooth.BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED" />
<action android:name="android.bluetooth.BluetoothDevice.ACTION_ACL_DISCONNECTED" />
</intent-filter>
</receiver>
编辑 2:
为什么?因为我希望我的应用程序只能在车内工作...所以当应用程序连接到汽车的免提装置时,应用程序将启动...断开连接 5 分钟后,应用程序将 "terminated".
您将要在 Service 中而不是 activity 中注册广播接收器,并且 return START_STICKY
在 onStartCommand
中即使在 activity 被杀死后也要保留它 运行。示例:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
当某些蓝牙设备已连接时,如何在我的 android phone 中启动应用程序?
我正在尝试使用广播接收器执行此操作,但我遇到一个问题...当我完成 activity(退出应用程序)时,广播接收器必须未注册...所以没有收听连接设备的方式...
如何在应用程序完成后让这个广播接收器保持活动状态?
谢谢
编辑:
public class AutostartReceiver extends BroadcastReceiver
{
/**
* @param context Context
* @param intent Intent
* @return void
*/
@Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
switch (action)
{
case BluetoothDevice.ACTION_ACL_CONNECTED:
Log.v("PAREK", "cnn");
break;
case BluetoothDevice.ACTION_ACL_DISCONNECTED:
Log.v("PAREK", "dnn");
break;
}
}
}
我也尝试了清单初始化
<receiver
android:name=".Model.Tools.AutostartReceiver">
<intent-filter>
<action android:name="android.bluetooth.BluetoothDevice.ACTION_ACL_CONNECTED" />
<action android:name="android.bluetooth.BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED" />
<action android:name="android.bluetooth.BluetoothDevice.ACTION_ACL_DISCONNECTED" />
</intent-filter>
</receiver>
编辑 2:
为什么?因为我希望我的应用程序只能在车内工作...所以当应用程序连接到汽车的免提装置时,应用程序将启动...断开连接 5 分钟后,应用程序将 "terminated".
您将要在 Service 中而不是 activity 中注册广播接收器,并且 return START_STICKY
在 onStartCommand
中即使在 activity 被杀死后也要保留它 运行。示例:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}