【问题标题】:Pick a variable from onHandleIntent从 onHandleIntent 中选择一个变量
【发布时间】:2015-10-04 01:11:06
【问题描述】:

我有一个服务类,我在“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();
    }





}

有人可以帮助我吗?谢谢

【问题讨论】:

    标签: android android-intent service intentservice oncreate


    【解决方案1】:
     mHandler = new Handler();
     mHandler.onHandleIntent(Intent Var);
    

    是的,当加载你的类时,.on create 是第一次运行该方法。所以,没有初始化'onHandleIntent'方法。所以,你在创建方法中调用那个方法。我认为这会解决你的问题。 注意:您将“Intent”对象作为参数传递。

    示例:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-22
      • 1970-01-01
      • 1970-01-01
      • 2015-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多