Welcome to XDA

Search to go directly to your device's forum

Register an account

Unlock full posting privileges

Ask a question

No registration required
Post Reply

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

OP student71

30th April 2014, 05:54 AM   |  #1  
OP Junior Member
Thanks Meter: 0
 
4 posts
Join Date:Joined: Apr 2014
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.
30th April 2014, 06:47 AM   |  #2  
Member
Thanks Meter: 3
 
36 posts
Join Date:Joined: 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.
30th April 2014, 07:28 AM   |  #3  
OP Junior Member
Thanks Meter: 0
 
4 posts
Join Date:Joined: 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>
1st May 2014, 06:22 AM   |  #4  
OP Junior Member
Thanks Meter: 0
 
4 posts
Join Date:Joined: Apr 2014
Any idea?
Post Reply Subscribe to Thread
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes