简体   繁体   English

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

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

I am passing intent to a service from two different locations in my application. 我正在将意图从应用程序中的两个不同位置传递给服务。 One intent has a bundle from which I want to extract data.But I get NPE when I try run getIntent().getExtras() method. 一个意图有一个我想从中提取数据的包,但是当我尝试运行getIntent()。getExtras()方法时得到了NPE。

EDIT: In one class: 编辑:在一个类中:

                        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);

In another class, 在另一堂课中

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);

And in service class, 在服务级别上

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.

相关问题 如何从意图中获取多张图片 - How to get multiple pictures from intent 如何阻止多个意图按顺序打开? - How to stop multiple intents from opening sequentially? Android Studio 如何从WallpaperManager 获取Intent 到Service - Android Studio how to get Intent into Service from WallpaperManager 在意图传递的同一包中使用putParcelableArrayList和putInt - use putParcelableArrayList and putInt on the same bundle passed in intent Android-具有待定意图的多个警报,如何确定正在调用哪个意图? - Android - Multiple alarms w/ pending intents, how to determine which intent is being called? 如何使用intent和bundle传递值 - how to pass a value using intents and bundle 如何使用Arraylist中的bundle使用putserializable获取传递的值 <Hashmap<String,String> &gt; - How to get valus passed using putserializable using bundle from Arraylist<Hashmap<String,String>> [Android] [Tab-Intent]如何从Intents打开Tabactivity中的特定标签? - [Android][Tab-Intent] How to open to a specific tab in tabactivity from Intents? 使用具有不同活动和最终电子邮件意图的多个意图 - Using Multiple intents with different activities and final email intent 在意图服务和类之间未传递值 - Values not passed between Intent Service and Class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM