我有一个服务类,我在方法“onHandleIntent”中从Intent获取数据并在变量中获取。我想在onCreate中调用这个变量的值,但是当我调用这个变量时,我得到一个NULL值。 (对于onHandleIntent中的例子,我有一个变量a = True,但在onCreate中这个变量不是真的)。我想把这个变量从onHandleIntent转到onCreate。这是我的代码:
public class LogService extends IntentService
{
private BluetoothAdapter mBluetoothAdapter;
private boolean mScanning;
private Handler mHandler;
SoundManager sm;
private boolean notifica = false;
private String id;
Boolean a = false;
private static final int REQUEST_ENABLE_BT = 1;
// Stops scanning after 10 seconds.
private static final long SCAN_PERIOD = 2100000000;
Handler handler;
public LogService()
{
super("LogService");
}
@Override
protected void onHandleIntent(Intent intent) {
id=(String) intent.getStringExtra("idc");
System.out.println("id1 e'" + id);
a = true;
}
@Override
public void onCreate() {
// ActiveBluetooth();
handler = new Handler();
super.onCreate();
// super.onCreate(savedInstanceState);
mHandler = new Handler();
System.out.println("Vero o falso?'" + a);
System.out.println("id e'" + id);
// Boolean a = getIntent().getExtras().getBoolean("ok");
// Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
//startActivities(enableBtIntent, REQUEST_ENABLE_BT);
// Use this check to determine whether BLE is supported on the device. Then you can
// selectively disable BLE-related features.
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
super.onDestroy();
}
// Initializes a Bluetooth adapter. For API level 18 and above, get a reference to
// BluetoothAdapter through BluetoothManager.
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
if (mBluetoothAdapter == null) {
Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show();
super.onDestroy();
return;
}
// Checks if Bluetooth is supported on the device.
//onResume();
// BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
/* if (!mBluetoothAdapter.isEnabled()) {
Toast.makeText(this, "Auto attivazione blueooth: Bluetooth attivato.", Toast.LENGTH_SHORT).show();
mBluetoothAdapter.enable();
scanLeDevice(true);
Intent refresh = new Intent(LogService.this, LogService.class);
startService(refresh);
// Intent refresh = new Intent(this, LogService.class);
//startService(refresh);
Toast.makeText(this, "Refresh", Toast.LENGTH_SHORT).show();
}*/
// if (!mBluetoothAdapter.isEnabled()) {
// Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
// ab.startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
//}
scanLeDevice(true);
}
/*
protected void onResume() {
// Ensures Bluetooth is enabled on the device. If Bluetooth is not currently enabled,
// fire an intent to display a dialog asking the user to grant permission to enable it.
if (!mBluetoothAdapter.isEnabled()) {
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
//startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
// Initializes list view adapter.
scanLeDevice(true);
}*/
private void scanLeDevice(final boolean enable) {
if (!mBluetoothAdapter.isEnabled() && notifica == false) {
Toast.makeText(this, "Auto attivazione blueooth: Bluetooth attivato.", Toast.LENGTH_SHORT).show();
mBluetoothAdapter.enable();
stopService(new Intent(this, LogService.class));
startService(new Intent(this, LogService.class));
}
if (enable) {
// Stops scanning after a pre-defined scan period.
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
}, SCAN_PERIOD);
mScanning = true;
mBluetoothAdapter.startLeScan(mLeScanCallback);
} else {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
}
// Device scan callback.
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Log.i("Trovato", "Evento n.");
System.out.println("TROVATO");
notifica = true;
notifica(device.getAddress());
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
//System.out.println(device.getAddress());
onDestroy();
// mBluetoothAdapter.stopLeScan(mLeScanCallback);
try {
Thread.sleep(10000000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
};
@Override
public void onDestroy()
{
Log.i("PROVA SERVICE", "Distruzione Service");
System.out.println("Distruzione");
// mScanning = false;
// mBluetoothAdapter.stopLeScan(mLeScanCallback); //� stato invocato il metodo onDestroy(), posso fermare la scansione
super.onDestroy();
}
private void runOnUiThread(Runnable runnable) {
handler.post(runnable);
}
private void notifica(String a) {
Log.i("Trovato", "Evento n.");
System.out.println("VAAAAAAAAA");
System.out.println("Indirizzo dispositivo" + a);
NotificationCompat.Builder n = new NotificationCompat.Builder(this);
// n.setLargeIcon(Imag);
n.setContentInfo("Affrettati!");
n.setContentTitle("La corriera e' in arrivo!");
n.setContentText(a);
n.setSmallIcon(R.drawable.bus);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, n.build());
sm.init(LogService.this);
sm.play();
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
Toast.makeText(this, "Auto disattivazione blueooth: Bluetooth disattivato.", Toast.LENGTH_SHORT).show();
mBluetoothAdapter.disable();
super.onDestroy();
}
}
有人能帮助我吗?感谢
答案 0 :(得分:0)
mHandler = new Handler();
mHandler.onHandleIntent(Intent Var);
是的,加载你的类.on create是第一次运行该方法。所以,没有初始化'onHandleIntent'方法。所以,你在create方法中调用该方法。我认为这将解决你的问题。 注意:您将'Intent'对象作为参数传递。
示例:
Intent i = new Intent(LogService.this);
mHandler = new Handler();
mHandler.onHandleIntent(i);