How to pass value from Activity to a Service in An

2019-04-01 20:50发布

I need access a simple int variable which holds an editText element value. The value is stored as a public field on my Activity class. On my Service, I created an object from my activity class:

CheckActivity check = new CheckActivity();

and I am trying to access it by:

check.getFirstPosition();

but it returns zero. What should I do to pass the value from an Activity to a Service?

3条回答
啃猪蹄的小仙女
2楼-- · 2019-04-01 20:59

You cannot create an object like that from your service. I think you are new to Java. When you do CheckActivity check = new CheckActivity() a new instance of your CheckActivity is created and no doubt it will return zero. Also you should never try creating objects of activities like this in android.

As far as your question is concerned you can pass your editText value to your service via a broadcast receiver.

Have a look at this.

Also if you have the editText value before creating the service you can simply pass it as intent extra , else you can use the broadcast approach.

In your service

broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if (action.equalsIgnoreCase("getting_data")) {
                    intent.getStringExtra("value")
                }
            }
        };

        IntentFilter intentFilter = new IntentFilter();
        // set the custom action
        intentFilter.addAction("getting_data"); //Action is just a string used to identify the receiver as there can be many in your app so it helps deciding which receiver should receive the intent. 
        // register the receiver
        registerReceiver(broadcastReceiver, intentFilter);

In your activity

Intent broadcast1 = new Intent("getting_data");
        broadcast.putExtra("value", editext.getText()+"");
        sendBroadcast(broadcast1);

Also declare your receiver in onCreate of activity and unregeister it in onDestroy

unregisterReceiver(broadcastReceiver);
查看更多
劳资没心,怎么记你
3楼-- · 2019-04-01 21:03

CheckActivity check = new CheckActivity();

Never do this. Use Intent to create an activity instead.

What should I do to pass the value from an Activity to a Service?

You can pass it via Intent using context.startService() method. Or, you can bind to it and pass a value via reference.

You can also consider using a BroadcastReceiver or Handler.

查看更多
4楼-- · 2019-04-01 21:13

You need to use intent for passing data between different Android components be it Activity or Service.

Intent intent = new Intent(this, YourService.class);
intent.putExtra("your_key_here", <your_value_here>); 

And then start your service like this -

startService(intent);

Now you can use onBind() or onStartCommand() (depends on how you are using your service) to use the intent passed as an argument

String editTextValue = intent.getStringExtra("your_key_here");

You can use editTextValue anywhere you want now.

查看更多
登录 后发表回答