AutoMinimize Activity Demo
Description:
This example of will allows us to minimize currently running activity and will notify about the activity status on notification bar.
Algorithm:
This example of will allows us to minimize currently running activity and will notify about the activity status on notification bar.
Algorithm:
1.) Create a new project by File-> New -> Android Project name it AutoMinimizeDemo.
2.) You will see some default code into your main.xml, strings.xml and android manifest file.
3.) Now add 2 buttons into your main.xml or write following into main.xml file:
2.) You will see some default code into your main.xml, strings.xml and android manifest file.
3.) Now add 2 buttons into your main.xml or write following into main.xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" >
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button android:id="@+id/start"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Start">
<requestFocus />
</Button>
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Start">
<requestFocus />
</Button>
<Button android:id="@+id/stop"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Stop">
</Button>
</LinearLayout>
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Stop">
</Button>
</LinearLayout>
4.) Your launcher activity AutoMinimizeDemo will have two functions OnCreate() and ShowNotification..
Steps:
1.) Create a project named AutoMinimizeDemo and set the information as stated in the image.
Build Target: Android 1.6
Application Name: AutoMinimizeDemo
Package Name: com.example. AutoMinimizeDemo
Activity Name: AutoMinimizeDemo
Min SDK Version: 4
Application Name: AutoMinimizeDemo
Package Name: com.example. AutoMinimizeDemo
Activity Name: AutoMinimizeDemo
Min SDK Version: 4
2.) Open AutoMinimizeDemo.java file and write following code there:
package com.example.AutoMinimizeDemo;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class AutoMinimizeDemo extends Activity {
private NotificationManager mNM;
Intent notificationIntent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
private NotificationManager mNM;
Intent notificationIntent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mNM =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationIntent = new Intent(this, AutoMinimizeDemo.class);
Button startButton = (Button) findViewById(R.id.start);
startButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
showNotification();
Intent startMain = newIntent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
}
});
Button stopButton = (Button) findViewById(R.id.stop);
stopButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
mNM.cancelAll();
finish();
}
});
}
private void showNotification() {
CharSequence text = "Activity is minimized";
Notification notification = new Notification(R.drawable.icon, text, System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,new Intent(this, AutoMinimizeDemo.class), 0);
notification.setLatestEventInfo(this, "Auto MinimizeDemo","Running", 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(1, notification);
}
}
notificationIntent = new Intent(this, AutoMinimizeDemo.class);
Button startButton = (Button) findViewById(R.id.start);
startButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
showNotification();
Intent startMain = newIntent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
}
});
Button stopButton = (Button) findViewById(R.id.stop);
stopButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
mNM.cancelAll();
finish();
}
});
}
private void showNotification() {
CharSequence text = "Activity is minimized";
Notification notification = new Notification(R.drawable.icon, text, System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,new Intent(this, AutoMinimizeDemo.class), 0);
notification.setLatestEventInfo(this, "Auto MinimizeDemo","Running", 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(1, notification);
}
}
3.) Compile and build the project.
4.) Run on 1.6 simulator for the output.
5.) As soon as you click on Start button activity will be minimized and you will be on device’s home screen.
6.) You will be able to see a notification message on notification bar.
7.) When you click on notification message activity will be resumed.
On clicking Stop button the notification will be stopped and demo app will be closed.
Output
No comments:
Post a Comment