在我的应用程序中,我在检查手机状态RINGING后使用广播接收器来激活服务类。在服务类中,我在从数据库检查状态标志后禁用Wifi和蓝牙。我可以改变Wifi状态;但是当我为蓝牙尝试相同的操作时会出现异常。
有人可以提出在服务中使用BluetoothAdapter的方法吗?
DatabaseServic是我的服务类,这是我在logcat中收到的主要异常......
java.lang.RuntimeException:无法创建服务com.android.radiationreducer.DatabaseService:java.lang.NullPointerException
if(mBluetoothAdapter.isEnabled())
{
System.out.println("inside bluetooth iffffffffffffffffffffffffffffff");
mBluetoothAdapter.disable();
Toast.makeText(DatabaseService.this, "BLUETOOTH DISABLED", Toast.LENGTH_SHORT).show();
myDB.execSQL("INSERT INTO bluetoothflag VALUES ('"+RESET+"');");
myDB.delete("bluetoothflag", null, null);
myDB.execSQL("INSERT INTO bluetoothflag VALUES ('"+RESET+"');");
myDB.execSQL("INSERT INTO bluetoothflag VALUES ('"+SET+"');");
System.out.println("inside bluetooth ifffffffffendddddddddd");
}
请建议以编程方式关闭蓝牙的任何其他方法......
答案 0 :(得分:-1)
您在服务中使用BluetoothAdapter! (而且它有效......)
您的错误位于代码中的其他位置:
java.lang.RuntimeException:无法创建服务 com.android.radiationreducer.DatabaseService:java.lang.NullPointerException
也许您忘了将服务添加到清单(常见错误)?