Friday, August 23, 2013
Background App Example in Android Programming
HI,
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:
main.xml:
<RelativeLayout
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"
>
<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"
/>
</RelativeLayout>
3.)
Write following into manifest file:
mainfest.xml
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
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">
<intent-filter>
<action
android:name="android.intent.action.MAIN"/>
<category
android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<service
android:enabled="true"android:name=".BackgroundService"/>
</application>
</manifest>
4)
Create and write following into src/BackgroundService.java:
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)Open
BackgroundAppExample.java file and write following code there:
BackgroundAppExample.java
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
;
}
}
6)
Compile and build the project.
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment