5,815,179 Members 38,411 Now Online
XDA Developers Android and Mobile Development Forum

Example of an app that runs as a service(and does some work periodically in a thread)

Tip us?
 
student71
Old
#1  
Junior Member - OP
Thanks Meter 0
Posts: 4
Join Date: Apr 2014
Default Example of an app that runs as a service(and does some work periodically in a thread)

Hi there,

Can you share please a link to simple example of an app that runs as a service(and does some work periodically in a thread) and started at boot? It should not be an activity. Thank you.
 
surlac
Old
#2  
Junior Member
Thanks Meter 1
Posts: 21
Join Date: Feb 2012
Not a link, but should be good to start:

AndroidManifest.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest package="your.package" android:versionCode="1" android:versionName="1.0" >
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

    <application android:icon="@drawable/icon" android:label="@string/app_name"                   android:theme="@android:style/Theme.NoDisplay">>
        <receiver android:name=".Receiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <service android:enabled="true" android:name=".YourService" />
    </application>
</manifest>
Here you receive broadcast (Intent) dispatched by the system on boot.
Receiver.java
Code:
public class Receiver extends BroadcastReceiver {
    @Override public void onReceive(Context context, Intent intent) {
        Intent i = new Intent(this, YourService.class);        
        startService(i);
     }
}
Service should do your work.
YourService.java
Code:
public class YourService extends Service {
    @Override public void onStart(Intent intent, int startId) {
        // do some work here        
    }
}
This is a simple implementation. To make it run periodically, try to use AlarmManager with IntentService.
 
student71
Old
#3  
Junior Member - OP
Thanks Meter 0
Posts: 4
Join Date: Apr 2014
I wrote an app that unfortunately doesn't start at boot. I am also aware (theoretically) of the change in policy of BroadcastReceiver from 3.1 version. But I could not catch what the point is, how I register the service from user activity?

public class AlarmScheduleActivity extends Activity {

// UI parameters
Button btnStart;
Button btnStop;

@override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

setContentView(R.layout.main);

btnStart = (Button)findViewById(R.id.button1);
btnStop = (Button)findViewById(R.id.button2);
}

public void btnStartSchedule(View v) {

try {
AlarmManager alarms = (AlarmManager) this
.getSystemService(Context.ALARM_SERVICE);

Intent intent = new Intent(getApplicationContext(),
AlarmReceiver.class);
intent.putExtra(AlarmReceiver.ACTION_ALARM,
AlarmReceiver.ACTION_ALARM);

final PendingIntent pIntent = PendingIntent.getBroadcast(this,
1234567, intent, PendingIntent.FLAG_UPDATE_CURRENT);

alarms.setRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis(), 2000, pIntent);

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public void btnCancelSchedules(View v) {

Intent intent = new Intent(getApplicationContext(),
AlarmReceiver.class);
intent.putExtra(AlarmReceiver.ACTION_ALARM,
AlarmReceiver.ACTION_ALARM);

final PendingIntent pIntent = PendingIntent.getBroadcast(this, 1234567,
intent, PendingIntent.FLAG_UPDATE_CURRENT);

AlarmManager alarms = (AlarmManager) this
.getSystemService(Context.ALARM_SERVICE);

alarms.cancel(pIntent);
}
}
public class AlarmReceiver extends BroadcastReceiver {

public static String ACTION_ALARM = "com.alarammanager.alaram";

@override
public void onReceive(Context context, Intent intent) {

Bundle bundle = intent.getExtras();
String action = bundle.getString(ACTION_ALARM);
if (action.equals(ACTION_ALARM)) {
Log.i("Alarm Receiver", "If loop");
Intent inService = new Intent(context,TaskService.class);
context.startService(inService);
}
}
}

public class BootBroadcastReceiver extends BroadcastReceiver {
@override
public void onReceive(Context context, Intent intent) {
Intent alarmIntent = new Intent("com.company.android.AlarmReceiver");
PendingIntent pi = PendingIntent.getBroadcast(context, 0, alarmIntent,
PendingIntent.FLAG_UPDATE_CURRENT);

AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKE UP, System.currentTimeMillis(),
2000, pi);
}
}

public class TaskService extends IntentService {
public TaskService() {
super("TaskService");
// TODO Auto-generated constructor stub
}
@override
protected void onHandleIntent(Intent arg0) {
// Do some task
Log.i("TaskService", "Service running: yes-yes-yes");
}
}
AndroidManifest.xml

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMP LETED"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".AlarmScheduleActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".BootBroadcastReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED " />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<receiver
android:name="AlarmReceiver"
androidrocess=":remote" >
</receiver>
<service android:name=".TaskService" >
</service>
</application>
 
student71
Old
#4  
Junior Member - OP
Thanks Meter 0
Posts: 4
Join Date: Apr 2014
Any idea?
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes