Virual recent Service

Source: Internet
Author: User

1. Mainactivity.java

Package Com.jianli.virtualrecent;import Android.app.activity;import Android.content.intent;import Android.content.sharedpreferences;import Android.os.bundle;public class Mainactivity extends Activity {private static Final string TAG = "Virtualrecent";p rivate static final String preferences_virtual_recent_service = "Preferences_virtual _recent_service ";p rivate static final String key_pref_virtual_recent_service_started =" Key_pref_virtual_recent_ Service_started "; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); SECLOG.E (TAG, "onCreate");} @Overrideprotected void OnDestroy () {SECLOG.E (TAG, "OnDestroy"); Super.ondestroy ();} @Overrideprotected void OnPause () {SECLOG.E (TAG, "OnPause"); Super.onpause ();} @Overrideprotected void Onresume () {SECLOG.E (TAG, "Onresume"); Sharedpreferences msharedpreferences = getsharedpreferences (Preferences_virtual_recent_service, Activity.MODE_ PRIVATE); Sharedpreferences.editor Editor= Msharedpreferences.edit (); Intent Intent = new Intent (this, virtualrecentservice.class); if ( Msharedpreferences.getboolean (key_pref_virtual_recent_service_started, false) = = False) {StartService (intent); Editor.putboolean (key_pref_virtual_recent_service_started, True);} Else{stopservice (intent); Editor.putboolean (key_pref_virtual_recent_service_started, false);} Editor.commit (); Finish (); Super.onresume ();}}


2. Virtualrecentservice.java

Package Com.jianli.virtualrecent;import Java.lang.reflect.field;import Android.annotation.suppresslint;import Android.app.activity;import Android.app.service;import Android.content.context;import android.content.Intent; Import Android.content.sharedpreferences;import Android.graphics.pixelformat;import Android.os.IBinder;import Android.view.gravity;import Android.view.layoutinflater;import Android.view.motionevent;import Android.view.View; Import Android.view.view.ontouchlistener;import Android.view.windowmanager;import Android.view.windowmanager.layoutparams;import Android.widget.imagebutton;import android.widget.LinearLayout;@ Suppresslint ("Inflateparams") public class Virtualrecentservice extends Service {private static final String TAG = "Virtua Lrecentservice ";p rivate static final String Preferences_virtual_recent_service =" Preferences_virtual_recent_service ";p rivate static final String key_preferences_vrs_window_x =" key_preferences_vrs_window_x ";p rivate static final string Key_preferences_vrs_window_y = "Key_preferences_vrs_window_y";p rivate static final String Key_preferences_vrs_statusbar_ HEIGHT = "Key_preferences_vrs_statusbar_height";p rivate static final String vrs_toggle_recent_intent = " Com.android.systemui.recent.action.TOGGLE_RECENTS ";p rivate sharedpreferences msharedpreferences;private LinearLayout mfloatlayout;private windowmanager.layoutparams wmparams;private windowmanager mWindowManager;private ImageButton mbtnimage;private static int mstatusbarheight = 0;private static float Mpreviousx = 0;private static float mPr Eviousy = 0;private static Boolean bismoved = false; @Overridepublic ibinder Onbind (Intent arg0) {seclog.e (TAG, "Onbind"); r Eturn null;} @Overridepublic void OnCreate () {SECLOG.E (TAG, "onCreate"); Super.oncreate ();} @Overridepublic void OnDestroy () {SECLOG.E (TAG, "OnDestroy"); if (mfloatlayout! = null) {Mwindowmanag        Er.removeview (mfloatlayout); }super.ondestroy ();} @Overridepublic int Onstartcommand (Intent Intent, int flags, int startid) {seclog.e (TAG, "Onstartcommand"); msharedpreferences = Getsharedpreferences (Preferences_virtual_ Recent_service, activity.mode_private); Layoutinflater inflater = Layoutinflater.from (Getapplication ()); mfloatlayout = (linearlayout) inflater.inflate ( R.layout.float_layout, null); mbtnimage = (ImageButton) Mfloatlayout.findviewbyid (r.id.btn_recent); mstatusbarheight        = Msharedpreferences.getint (key_preferences_vrs_statusbar_height, 0);        if (0 = = mstatusbarheight) {mstatusbarheight = Getstatusbarheight (Getbasecontext ()); Sharedpreferences.editor Editor = Msharedpreferences.edit (); Editor.putint (Key_preferences_vrs_statusbar_height,        Mstatusbarheight); Editor.commit (); }createfloatview (); bismoved = False;return Super.onstartcommand (Intent, flags, Startid);} private void Createfloatview () {SECLOG.E (TAG, "Createfloatview"); wmparams = new Windowmanager.layoutparams (); Mwindowmanager = (WindowManager) getapplication (). Getsystemservice (Getapplication (). Window_serviCE); wmparams.type = Layoutparams.type_phone;wmparams.format = Pixelformat.rgba_8888;wmparams.flags = layoutparams.flag_not_focusable;wmparams.gravity = Gravity.left | Gravity.top;wmparams.x = Msharedpreferences.getint (key_preferences_vrs_window_x, 0); wmParams.y = Msharedpreferences.getint (key_preferences_vrs_window_y, 0); mpreviousx = Wmparams.x;mpreviousy = WmParams.y; Wmparams.width = 77;wmparams.height = 77;mwindowmanager.addview (Mfloatlayout, wmparams); Mbtnimage.setontouchlistener (New Ontouchlistener () {@Overridepublic Boolean OnTouch (View arg0, motionevent event) { SECLOG.E (Tag, "OnTouch"), switch (event.getactionmasked ()) {case MOTIONEVENT.ACTION_DOWN:SECLOG.E (tag, "OnTouch, Action_down "); Mpreviousx = Event.getrawx (); mpreviousy = Event.getrawy (); Break;case MOTIONEVENT.ACTION_MOVE:SECLOG.E (TAG, "OnTouch, Action_move"); if (Math.Abs (MPREVIOUSX-EVENT.GETRAWX ()) < && Math.Abs (Mpreviousy- Event.getrawy ()) <) return false;bismoved = True;wmparams.x = (int) (EVENT.GETRAWX () -Mfloatlayout.getmeasuredwidth ()/2); wmparams.y = (int) (Event.getrawy ()-Mstatusbarheight- Mfloatlayout.getmeasuredheight ()/2); Mwindowmanager.updateviewlayout (Mfloatlayout, wmparams); break;case MOTIONEVENT.ACTION_UP:SECLOG.E (TAG, "OnTouch, action_up"); if (true = = bismoved) {bismoved = false; Sharedpreferences.editor Editor = Msharedpreferences.edit (); Editor.putint (key_preferences_vrs_window_x, wmParams.x ); Editor.putint (key_preferences_vrs_window_y, Wmparams.y); Editor.commit (); return false;} Intent Intent = new Intent (vrs_toggle_recent_intent); Intent.setclassname ("Com.android.systemui", " Com.android.systemui.recent.RecentsActivity "); Intent.setflags (Intent.flag_activity_new_task | intent.flag_activity_exclude_from_recents); startactivity (Intent); BREAK;DEFAULT:SECLOG.E (TAG, "onTouch,event=" + Event.getactionmasked ()); break;} return false;}}); @Overridepublic boolean onunbind (Intent Intent) {seclog.e (TAG, "Onunbind"); return Super.onunbind (Intent);} public static int Getstatusbarheight (Context cOntext) {SECLOG.E (TAG, "getstatusbarheight");        Class<?> c = null;        Object obj = null;        Field field = NULL;        int x = 0, statusbarheight = 0;            try {c = class.forname ("Com.android.internal.r$dimen");            obj = C.newinstance ();            field = C.getfield ("Status_bar_height");            x = Integer.parseint (Field.get (obj). toString ());        Statusbarheight = Context.getresources (). getdimensionpixelsize (x);        } catch (Exception E1) {e1.printstacktrace ();    } return statusbarheight; }}


3. Virtualrecentbootreceiver.java

Package Com.jianli.virtualrecent;import Android.app.activity;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import Android.content.sharedpreferences;import Android.widget.toast;public class Virtualrecentbootreceiver extends Broadcastreceiver {private static final String TAG = "Virtualrecentbootreceiver";p rivate static final String Preferences_virtual_recent_service = "Preferences_virtual_ Recent_service ";p rivate static final String key_pref_virtual_recent_service_started =" Key_pref_virtual_recent_ Service_started "; @Overridepublic void OnReceive (context context, Intent arg1) {seclog.e (TAG," onreceive "); Toast.maketext (Context, "Boot complete", Toast.length_long). Show (); Intent Intent = new Intent (context, Virtualrecentservice.class); Context.startservice (intent); Sharedpreferences msharedpreferences = context.getsharedpreferences (Preferences_virtual_recent_service, Activity.mode_private); Sharedpreferences.editor Editor = MsharedpreferencEs.edit (); Editor.putboolean (key_pref_virtual_recent_service_started, True); Editor.commit ();}} 


4. Manifest.java

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Com.jianli.virtualrecent "android:versioncode=" 1 "android:versionname=" 1.0 "> &LT;USES-SD K android:minsdkversion= "8" android:targetsdkversion= "/> <uses-permission android:name=" Android . permission. Receive_boot_completed "/> <uses-permission android:name=" Android.permission.SYSTEM_ALERT_WINDOW "/> <a Pplication android:allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@string/app _name "> <activity android:name=".            Mainactivity "Android:theme=" @android: Style/theme.nodisplay "android:label=" @string/app_name "> <intent-filter> <action android:name= "Android.intent.action.MAIN"/> & Lt;category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity> <service android:name= ".            Virtualrecentservice "> </service> <receiver android:name=" Virtualrecentbootreceiver ">                <intent-filter > <action android:name= "Android.intent.action.BOOT_COMPLETED" > </action> </intent-filter> </receiver> </application></manifest>


5. Activity_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 "    tools:context= "${relativepackage}.${activityclass}" ></RelativeLayout>


6. Float_layout.xml

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "vertical" >        <imagebutton    android:id= "@+id/btn_recent"    android:src= "@drawable/ Recent_button "        android:layout_width=" wrap_content "        android:layout_height=" Wrap_content "/></ Linearlayout>


Virual recent Service

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.