Android How to send activity data to a service?

In my application, I am trying to send data from my MainActivity.class to a service called bgservice.class . This is my following code:

MainActivity.class:

 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent serviceIntent = new Intent(bgservice.class.getName()); serviceIntent.putExtra("UserID", "123456"); this.startService(serviceIntent); } 

bgservice.class:

 @Override public int onStartCommand(Intent intent, int flags, int startId) { String userID = intent.getStringExtra("userID"); System.out.println(userID); Toast.makeText(this,userID, Toast.LENGTH_LONG).show(); return START_STICKY; } 

but i am not getting data in service class. this is the following error i get:

02-25 09: 05: 41.166: E / AndroidRuntime (2633):

java.lang.RuntimeException: cannot start activity
ComponentInfo {com.microapple.googleplace / com.microapple.googleplace. MainActivity}: java.lang.IllegalArgumentException: the intent of the service must be explicit: Intent {act = com.microapple.googleplace.bgservice (has additional features)}

AndroidManifest.xml:

  ... <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:enabled="true" android:name=".bgservice" /> </application> .... 
+6
source share
2 answers

Here:

  Intent serviceIntent = new Intent(bgservice.class.getName()); 

Passing a string to the Intent constructor to create an Intent to start the service. The Intent constructor accepts String as the name of the action we added to AndroidManifest.xml .

 <service android:enabled="true" android:name=".bgservice"> <intent-filter > <action android:name="com.microapple.googleplace.bgservice" /> </intent-filter> </service> 

Now use com.microapple.googleplace.bgservice as the name of the action to create the Intent:

  Intent serviceIntent = new Intent("com.microapple.googleplace.bgservice"); serviceIntent.putExtra("UserID", "123456"); this.startService(serviceIntent); 

OR

Use an Intent constrictor that takes a context as the first parameter and the name of the component that we want to run as the second parameter:

  Intent serviceIntent = new Intent(this,bgservice.class); serviceIntent.putExtra("UserID", "123456"); this.startService(serviceIntent); 

And also use the same key that is used to add data to the Intent, currently adding the value using the UserID , but trying to get the value using the UserID key

+3
source
 Intent serviceIntent = new Intent(YourActivity.this, bgservice.class); serviceIntent.putExtra("UserID", "123456"); this.startService(serviceIntent); 

And at your service

 public int onStartCommand(Intent intent, int flags, int startId) { String userID = intent.getStringExtra("UserID"); //do something return START_STICKY; } 

That should work.

+2
source

Source: https://habr.com/ru/post/982972/


All Articles