繁体   English   中英

如果多个意图传递给服务,如何从意图中获取捆绑

[英]How to get bundle from an intent if multiple intents are passed to the service

我正在将意图从应用程序中的两个不同位置传递给服务。 一个意图有一个我想从中提取数据的包,但是当我尝试运行getIntent()。getExtras()方法时得到了NPE。

编辑:在一个类中:

                        basket.putString("KEY1", id[i]);
                        basket.putString("KEY2", message[i]);
                        basket.putString("KEY3", timeFormat[i]);
                        passNotiData = new Intent(getActivity(),
                                CheckService.class);
                        passNotiData.putExtras(basket);
                        startService(passNotiData);

在另一堂课中

Intent myIntent = new Intent(context, AlarmReceiver.class);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, myIntent, 0);
     context.startService(myIntent);

    mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
            SystemClock.elapsedRealtime() + 60000, PERIOD, pi);

在服务级别上

Bundle gotBasket = intent.getExtras();
        oldId = gotBasket.getString("KEY1");
        oldTime = gotBasket.getString("KEY3");
//start service from activity or any place
Intent intent = new Intent(this, MyService.class);
            Bundle bundle = new Bundle();
            bundle.putString("TEST", "test got it.");
            intent.putExtras(bundle);
            startService(intent);


//In service class declare override method of service
@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        if (intent != null && intent.getExtras() != null) {
            String str = intent.getExtras().getString("TEST");
            Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG)
                    .show();
        }
        return START_NOT_STICKY;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM