2015-10-04 54 views
-2

我有一個服務類,我從方法「onHandleIntent」中的Intent中獲取數據並獲取一個變量。我會在onCreate中調用這個變量的值,但是當我調用這個變量時,我得到一個NULL值。 (對於onHandleIntent中的實例,我有一個變量a = True,但是在onCreate中,這個變量不是真的)。我想從onHandleIntent的這個變量到onCreate。這是我的代碼:從onHandleIntent中選擇一個變量

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
mHandler = new Handler(); 
mHandler.onHandleIntent(Intent Var); 

是的,當加載你的類.on創建是第一次運行該方法。所以,沒有初始化'onHandleIntent'method.so,你調用該方法,內創建method.I認爲這將解決您的問題。 注意:您將'Intent'對象作爲參數傳遞。

例如:

Intent i = new Intent(LogService.this); 
mHandler = new Handler(); 
mHandler.onHandleIntent(i);