In this function, we can pass class as parameter
Definition
/** * check the given service is running * @param serviceClass class eg MyService.class * @return boolean */ private boolean isMyServiceRunning(Class<?> serviceClass) { ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { if (serviceClass.getName().equals(service.service.getClassName())) { return true; } } return false; }
Function call
isMyServiceRunning(MyService.class);
Here we can pass the class name with the package name
Definition
/** * check the given service is running * @param serviceClassname this shoul be the service class name with the package eg "com.example.eg.intentserviceexample.MyService" * @return boolean */ private boolean isMyServiceRunning(String serviceClassname) { //getting all the services ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE); for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { Log.i("debug", service.service.getClassName()); if (serviceClassname.equals(service.service.getClassName())) { return true; } } return false; }
calling the function
isMyServiceRunning("com.example.eg.intentserviceexample.MyService");
Simple intent service example click the link https://wiki.workassis.com/android-intentservice-example
Simple example for Android Intent Service and broadcast receiver
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.eg.intentserviceexample"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".MyService" android:exported="false"/> </application> </manifest>
android:exported :- Whether or not components of other applications can invoke the service or interact with it — “true
” if they can, and “false
” if not. When the value is “false
“, only components of the same application or applications with the same user ID can start the service or bind to it. – developer.android.com
More about service tag click here
package com.example.eg.intentserviceexample; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.support.v4.content.LocalBroadcastManager; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //setting button click findViewById(R.id.btn_start_service).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //Creating an intent for sending to service Intent intent = new Intent(getApplicationContext(), MyService.class); intent.putExtra("id", 101); intent.putExtra("msg", "hi"); //starting service startService(intent); } }); } @Override protected void onStart() { super.onStart(); //register broadcast receiver for the intent MyTaskStatus LocalBroadcastManager.getInstance(this).registerReceiver(MyReceiver, new IntentFilter("MyServiceStatus")); } //Defining broadcast receiver private BroadcastReceiver MyReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String message = intent.getStringExtra("serviceMessage"); Toast.makeText(MainActivity.this, "Received : " + message, Toast.LENGTH_SHORT).show(); } }; @Override protected void onStop() { super.onStop(); LocalBroadcastManager.getInstance(this).unregisterReceiver(MyReceiver); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.eg.intentserviceexample.MainActivity" android:orientation="vertical"> <Button android:id="@+id/btn_start_service" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Start Service"/> </LinearLayout>
package com.example.eg.intentserviceexample; import android.app.IntentService; import android.content.Intent; import android.support.v4.content.LocalBroadcastManager; import android.util.Log; public class MyService extends IntentService { public MyService() { super(MyService.class.getName()); } @Override protected void onHandleIntent(Intent intent) { //retrieving data from the received intent int id = intent.getIntExtra("id",0); String message = intent.getStringExtra("msg"); Log.i("Data ", "id : "+id+" message : "+ message ); //----------------------------------------------- //Do your long running task here //------------------------------------------------ //Broadcasting some data Intent myIntent = new Intent("MyServiceStatus"); myIntent.putExtra("serviceMessage", "Task done"); // Send broadcast LocalBroadcastManager.getInstance(this).sendBroadcast(myIntent); } }
For How to check the service is running click the link : https://wiki.workassis.com/android-check-the-service-is-running
ref:
https://developer.android.com/reference/android/app/IntentService.html
https://developer.android.com/training/run-background-service/create-service.html
https://developer.android.com/guide/topics/manifest/service-element.html