I have created a Service
in my android application which starts automatically on BOOT_COMPLETE
through BroadcastReceiver
. And That is working perfectly fine. But this service performs the task that I have define inside onCreate()
method, only once. On the Other hand I want to run the Service
forever in background. Actually inside onCreate()
method I am reading data from my database and I am generating notifications if required. And the notifications can be generated any time, so therefore I want to run my service forever. I am new to Android, and I have seen may examples & tutorial but they did not helped. So kindly Answer that how can I run my Service
forever.
This is code of Service:
package com.example.abc.project1;
import android.app.*;
import android.content.Intent;
import android.os.IBinder;
import android.util.*;
import org.json.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class HelloService extends Service {
private static final String TAG = "HelloService";
public static boolean isRunning = false;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
isRunning = true;
new Thread(new Runnable() {
@Override
public void run() {
/*Here I want to do my task forever (reading from database & generate notifications)*/
}
}).start();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return Service.START_STICKY;
}
@Override
public void onDestroy() {
isRunning = false;
}
}
This is code of BroadcastReceiver
package com.example.abc.project1;
import android.content.*;
import android.util.Log;
public class MyBroadcastreceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, HelloService.class);
context.startService(startServiceIntent);
}
}
This is part of AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.abc.project1">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gef.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_CATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<service android:name=".HelloService"></service>
<receiver android:name="com.example.abc.project1.MyBroadcastreceiver" android:enabled="true" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
</application>
</manifest>