Tuesday 14 May 2013

Autominimize Activity Example

1)create class AutoMinimizeDemo.java
=======================
package com.ovte.autominimizeactivitydemo;

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.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class AutoMinimizeDemo extends Activity {
    private String TAG=getClass().getName();
    private NotificationManager mNM;
    private 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)
            {
                Activity_Minimize();
            }
        });
        
//        Button stopButton = (Button) findViewById(R.id.stop);
//        stopButton.setOnClickListener(new OnClickListener()
//        {
//            @Override
//            public void onClick(View v)
//            {
//                mNM.cancelAll();
//                finish();
//            }
//        });
//        Log.i(TAG, "onCreate");
    }
   
    @Override
    protected void onResume() {
        super.onResume();
        Log.i(TAG, "onResume");
    }
   
    @Override
    protected void onPause() {
        super.onPause();
        if(!isFinishing()){
            Activity_Minimize();
        }
        Log.i(TAG, "onPause");
    }
   
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy");
    }
   
    @Override
    protected void onRestart() {
        super.onRestart();
        mNM.cancelAll();
        showNotification();
        Log.i(TAG, "onRestart");
    }
   
    @Override
    protected void onStart() {
        super.onStart();
        Log.i(TAG, "onStart");
    }
   
    @Override
    protected void onStop() {
        super.onStop();
        Log.i(TAG, "onStop");
    }
   
    private void Activity_Minimize(){
        showNotification();
        Intent startMain = new Intent(Intent.ACTION_MAIN);       
        startMain.addCategory(Intent.CATEGORY_HOME); 
//        startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);       
        startActivity(startMain);
    }
    
     private void showNotification() {
            CharSequence text = "Activity is minimized";
            Notification notification = new Notification(R.drawable.ic_launcher, text, System.currentTimeMillis());
            PendingIntent contentIntent = PendingIntent.getActivity(this,0 ,notificationIntent,Intent.FLAG_ACTIVITY_NEW_TASK);
            notification.setLatestEventInfo(this, "Auto MinimizeDemo","Running", contentIntent);
            notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_NO_CLEAR;   
            mNM.notify(1, notification);
     }
}

2)main.xml
==========
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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>

    <Button android:id="@+id/stop"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="Stop">
    </Button>
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
     />
    
</LinearLayout>
3)manifest.xml
==========
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ovte.autominimizeactivitydemo"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        >
        <activity
            android:name="com.ovte.autominimizeactivitydemo.AutoMinimizeDemo"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:launchMode="singleTask"
            android:excludeFromRecents="false"
            android:configChanges="keyboardHidden|orientation" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

No comments:

Post a Comment