Algorithm:
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:
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" >
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="Stop">
</Button>
</LinearLayout>
Application Name: AutoMinimizeDemo
Package Name: com.example. AutoMinimizeDemo
Activity Name: AutoMinimizeDemo
Min SDK Version: 4
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;
private NotificationManager mNM;
Intent notificationIntent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
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);
}
}