Background App Example In Android Programming

This example shows how you can minimize your activity and start a service and resume your activity whenever required.

Algorithm:

1.) Create a new project by File-> New -> Android Project name it BackgroundAppExample.
2.) Write following into main.xml:

 

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="@dimen/padding_medium"
android:text="@string/hello_world"
tools:context=".BackgroundAppExample" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/textView1"
android:layout_marginRight="49dp"
android:layout_marginTop="74dp"
android:text="Start Service" />

 

 

3.) Write following into manifest file:

 

package="com.example.backgroundappexample"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".BackgroundAppExample"
android:label="@string/title_activity_background_app_example" >

 

 

4.) Create and write following into src/BackgroundService.java:

 

package com.example.backgroundappexample;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;

public class BackgroundService extends Service {
private NotificationManager mNM;
Bundle b;
Intent notificationIntent;
private final IBinder mBinder = new LocalBinder();
private String newtext;

public class LocalBinder extends Binder {
BackgroundService getService() {
return BackgroundService.this;
}
}

@Override
public void onCreate() {
mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

newtext = "BackGroundApp Service Running";

Notification notification = new Notification(R.drawable.ic_launcher, newtext,System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(BackgroundService.this, 0, new Intent(BackgroundService.this, BackgroundAppExample.class), 0);
notification.setLatestEventInfo(BackgroundService.this,"BackgroundAppExample", newtext, contentIntent);
mNM.notify(R.string.local_service_started, notification);
notificationIntent = new Intent(this, BackgroundAppExample.class);
showNotification();
}

public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
public void onDestroy() {
mNM.cancel(R.string.local_service_started);
stopSelf();
}
private void showNotification() {
CharSequence text = getText(R.string.local_service_started);

Notification notification = new Notification(R.drawable.ic_launcher, text, System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,new Intent(this, BackgroundAppExample.class), 0);
notification.setLatestEventInfo(this, "BackgroundAppExample",newtext, contentIntent);
notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

mNM.notify(R.string.local_service_started, notification);
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
}

 

 

5.) Run for output.

Steps:

1.) Create a project named BackgroundAppExample and set the information as stated in the image.

Build Target: Android 4.0
Application Name: BackgroundAppExample
Package Name: com. example. BackgroundAppExample
Activity Name: BackgroundAppExample
Min SDK Version: 8

2.) Open BackgroundAppExample.java file and write following code there:

 

package com.example.backgroundappexample;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class BackgroundAppExample extends Activity {

public static boolean isService = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button startserviceButton = (Button) findViewById(R.id.button1);
startserviceButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startService(new Intent(BackgroundAppExample.this,BackgroundService.class));
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
isService = true;
}
});
}

@Override
protected void onResume() {
super.onResume();
stopService(new Intent(BackgroundAppExample.this,
BackgroundService.class));
if(isService)
{
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("Service Resumed");
isService = false;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

 

 

3.) Compile and build the project.

Output

Leave a Comment:

4 comments
a.moeid says November 28, 2012

gud work

Reply
Frank says March 1, 2013

Hi,

this does not work for me. Could you please add this complete example as zip-archive here?

Thanks.
Frank

Reply
Anenth says March 24, 2013

Thanks for this tutorial, it was helpful 🙂

Reply
Prabhu says March 24, 2013

Thanks. It is very useful

Reply
Add Your Reply