长按home键显示当前任务

公司给我的任务就是怎么长按home键显示当前任务,刚开始我还以为利用一个activity开启一个service就行啦,然后在监听home键,但是监听到了home键,也是不能在service中显示一个view的,view是建立activity的基础上才能显示的,所以必须在一个activity中监听到home键的长按才行,所以最终只能在Launcher中监听home键的长按才行,那怎么监听home键的长按呢,其实我现在想到了两种办法,第一种是监听logcat消息日志,因为每次按下home键都会发送一个系统日志;第二种方法就是在Launcher中监听home键,方法在我的日志中。接着就是要重写一个dialog按钮,然后在里面显示最近用的应用程序,dialog的代码如下:

Recentapplicationsdialog.java代码   收藏代码
  1. public class RecentApplicationsDialog extends Dialog implements OnClickListener {  
  2.     // Elements for debugging support  
  3.     private static final String LOG_TAG = "RecentApplicationsDialog";  
  4.     private static final boolean DBG_FORCE_EMPTY_LIST = false;  
  5.   
  6.     private static final int NUM_BUTTONS = 8;  
  7.     private static final int MAX_RECENT_TASKS = NUM_BUTTONS * 2;    // allow for some discards  
  8.     private int mIconSize;  
  9.       
  10.     //public  static boolean RecentDialog_First = false ;   // ratation flag  
  11.       
  12.     final TextView[] mIcons = new TextView[NUM_BUTTONS];  
  13.     View mNoAppsText;  
  14.     IntentFilter mBroadcastIntentFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);  
  15.   
  16.     Handler mHandler = new Handler();  
  17.   
  18.     Runnable mCleanup = new Runnable() {  
  19.         public void run() {  
  20.             // dump extra memory we're hanging on to  
  21.             for (TextView icon: mIcons) {  
  22.                 icon.setCompoundDrawables(null, null, null, null);  
  23.                 icon.setTag(null);  
  24.             }  
  25.         }  
  26.     };  
  27.   
  28.      
  29.   
  30.     public RecentApplicationsDialog(Context context) {  
  31.         super(context, R.style.RecentApplicationsDialog);  
  32.         final Resources resources = context.getResources();  
  33.         mIconSize = (int) resources.getDimension(R.dimen.app_icon_size);  
  34.     }  
  35.   
  36.     /**  
  37.      * We create the recent applications dialog just once, and it stays around (hidden)  
  38.      * until activated by the user.  
  39.      *  
  40.      * @see PhoneWindowManager#showRecentAppsDialog  
  41.      */  
  42.     @Override  
  43.     protected void onCreate(Bundle savedInstanceState) {  
  44.         super.onCreate(savedInstanceState);   
  45.         Context context = getContext();  
  46.           
  47.           
  48.           
  49.         Window window = getWindow();  
  50.         window.requestFeature(Window.FEATURE_NO_TITLE);  
  51.         //window.setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);  
  52.         window.setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,  
  53.                 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);//导致界面里面所有需要弹出软键盘的控件均无法显示软键盘  
  54.         //window.setTitle("Recents");  
  55.   
  56.         setContentView(R.layout.recent_apps_dialog);  
  57.   
  58.         final WindowManager.LayoutParams params = window.getAttributes();  
  59.         params.dimAmount = 0.5f;//设置透明度(范围是0~1.0f)  
  60.           
  61.         DisplayMetrics display = new DisplayMetrics();    
  62.         window.getWindowManager().getDefaultDisplay().getMetrics(display);  
  63.   
  64.         WindowManager wm = (WindowManager)context.getSystemService(context.WINDOW_SERVICE);  
  65.         Display display_rotation = wm.getDefaultDisplay();  
  66.   
  67.         params.width = WindowManager.LayoutParams.MATCH_PARENT;  
  68.         params.height = WindowManager.LayoutParams.MATCH_PARENT;  
  69.   
  70.         window.setAttributes(params);  
  71.         window.setFlags(0, WindowManager.LayoutParams.FLAG_DIM_BEHIND);//遮罩式(模式)顶级窗口--该顶级view下的部件被遮住,不可以操作  
  72.           
  73.           
  74.           
  75.         mIcons[0] = (TextView)findViewById(R.id.button01);  
  76.         mIcons[1] = (TextView)findViewById(R.id.button02);  
  77.         mIcons[2] = (TextView)findViewById(R.id.button03);  
  78.         mIcons[3] = (TextView)findViewById(R.id.button04);  
  79.         mIcons[4] = (TextView)findViewById(R.id.button05);  
  80.         mIcons[5] = (TextView)findViewById(R.id.button06);  
  81.         mIcons[6] = (TextView)findViewById(R.id.button07);  
  82.         mIcons[7] = (TextView)findViewById(R.id.button08);  
  83.           
  84.         mNoAppsText = findViewById(R.id.no_applications_message);  
  85.   
  86.         for (TextView b: mIcons) {  
  87.             b.setOnClickListener(this);  
  88.         }  
  89.           
  90.     }  
  91.   
  92.     /**  
  93.      * Handler for user clicks.  If a button was clicked, launch the corresponding activity.  
  94.      */  
  95.     public void onClick(View v) {  
  96.         for (TextView b: mIcons) {  
  97.             if (b == v) {  
  98.                 // prepare a launch intent and send it  
  99.                 Intent intent = (Intent)b.getTag();  
  100.                 if (intent != null) {  
  101.                     intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);  
  102.                     try {  
  103.                         getContext().startActivity(intent);  
  104.                     } catch (ActivityNotFoundException e) {  
  105.                         Log.w("Recent""Unable to launch recent task", e);  
  106.                     }  
  107.                 }  
  108.                 break;  
  109.             }  
  110.         }  
  111.         dismiss();  
  112.     }  
  113.   
  114.     /**  
  115.      * Set up and show the recent activities dialog.  
  116.      */  
  117.     @Override  
  118.     public void onStart() {  
  119.         super.onStart();  
  120.         reloadButtons();  
  121.         //注册广播  
  122.         getContext().registerReceiver(mBroadcastReceiver, mBroadcastIntentFilter);  
  123.         //Activity不进行icons初始化工作  
  124.         mHandler.removeCallbacks(mCleanup);  
  125.     }  
  126.   
  127.     /**  
  128.      * Dismiss the recent activities dialog.  
  129.      */  
  130.     @Override  
  131.     public void onStop() {  
  132.         super.onStop();  
  133.         // stop receiving broadcasts  
  134.         getContext().unregisterReceiver(mBroadcastReceiver);  
  135.   
  136.         mHandler.postDelayed(mCleanup, 100);  
  137.      }  
  138.   
  139.   
  140.     /**  
  141.      * Reload the 6 buttons with recent activities  
  142.      */  
  143.     private void reloadButtons() {  
  144.   
  145.         final Context context = getContext();  
  146.           
  147.         final PackageManager pm = context.getPackageManager();  
  148.           
  149.         final ActivityManager am = (ActivityManager)  
  150.                 context.getSystemService(Context.ACTIVITY_SERVICE);  
  151.           
  152.         final List<ActivityManager.RecentTaskInfo> recentTasks =  
  153.                 am.getRecentTasks(MAX_RECENT_TASKS, ActivityManager.RECENT_WITH_EXCLUDED);  
  154.   
  155.         ActivityInfo homeInfo =   
  156.             new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME)  
  157.                     .resolveActivityInfo(pm, 0);  
  158.   
  159.         IconUtilities iconUtilities = new IconUtilities(getContext());  
  160.   
  161.         // Performance note:  Our android performance guide says to prefer Iterator when  
  162.         // using a List class, but because we know that getRecentTasks() always returns  
  163.         // an ArrayList<>, we'll use a simple index instead.  
  164.         int index = 0;  
  165.         int numTasks = recentTasks.size();  
  166.           
  167.         for (int i = 0; i < numTasks && (index < NUM_BUTTONS); ++i) {  
  168.               
  169.             final ActivityManager.RecentTaskInfo info = recentTasks.get(i);  
  170.   
  171.             // for debug purposes only, disallow first result to create empty lists  
  172.             if (DBG_FORCE_EMPTY_LIST && (i == 0)) continue;  
  173.   
  174.             Intent intent = new Intent(info.baseIntent);  
  175.             if (info.origActivity != null) {  
  176.                 intent.setComponent(info.origActivity);  
  177.             }  
  178.   
  179.             // Skip the current home activity.  
  180.             if (homeInfo != null) {  
  181.                 if (homeInfo.packageName.equals(  
  182.                         intent.getComponent().getPackageName())  
  183.                         && homeInfo.name.equals(  
  184.                                 intent.getComponent().getClassName())) {  
  185.                     continue;  
  186.                 }  
  187.             }  
  188.   
  189.             intent.setFlags((intent.getFlags()&~Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED)  
  190.                     | Intent.FLAG_ACTIVITY_NEW_TASK);  
  191.               
  192.             final ResolveInfo resolveInfo = pm.resolveActivity(intent, 0);  
  193.               
  194.             if (resolveInfo != null) {  
  195.                 final ActivityInfo activityInfo = resolveInfo.activityInfo;  
  196.                 final String title = activityInfo.loadLabel(pm).toString();  
  197.                 Drawable icon = activityInfo.loadIcon(pm);  
  198.   
  199.                 if (title != null && title.length() > 0 && icon != null) {  
  200.                     final TextView tv = mIcons[index];  
  201.                     tv.setText(title);  
  202.                     icon = iconUtilities.createIconDrawable(icon);  
  203.                     tv.setCompoundDrawables(null, icon, null, null);  
  204.                     tv.setTag(intent);  
  205.                     tv.setVisibility(View.VISIBLE);  
  206.                     tv.setPressed(false);  
  207.                     tv.clearFocus();  
  208.                     ++index;  
  209.                 }  
  210.             }  
  211.         }  
  212.   
  213.         // handle the case of "no icons to show"  
  214.         mNoAppsText.setVisibility((index == 0) ? View.VISIBLE : View.GONE);  
  215.   
  216.         // hide the rest  
  217.         for (; index < NUM_BUTTONS; ++index) {  
  218.             mIcons[index].setVisibility(View.GONE);  
  219.         }  
  220.           
  221.     }  
  222.   
  223.     /**  
  224.      * This is the listener for the ACTION_CLOSE_SYSTEM_DIALOGS intent.  It's an indication that  
  225.      * we should close ourselves immediately, in order to allow a higher-priority UI to take over  
  226.      * (e.g. phone call received).  
  227.      */  
  228.     private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {  
  229.         @Override  
  230.         public void onReceive(Context context, Intent intent) {  
  231.             String action = intent.getAction();  
  232.             if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {  
  233.                     dismiss();  
  234.             }  
  235.         }  
 
Androidmanifest.xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="cn.flyaudio.andriod"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <uses-sdk android:minSdkVersion="8" />  
  7.     <!-- 获取任务权限 -->  
  8.     <uses-permission android:name="android.permission.GET_TASKS"/>  
  9.     <!-- 加权限限制Home键 -->    
  10.     <uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>  
  11.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  12.         <activity android:name=".MainActivity"  
  13.                   android:label="@string/app_name">  
  14.             <intent-filter>  
  15.                 <action android:name="android.intent.action.MAIN" />  
  16.                 <category android:name="android.intent.category.LAUNCHER" />  
  17.                 <category android:name="android.intent.category.HOME" />  
  18.                 <category android:name="android.intent.category.DEFAULT" />  
  19.                 <category android:name="android.intent.category.MONKEY"/>  
  20.             </intent-filter>  
  21.         </activity>  
  22.         <service android:name=".HomeService"></service>  
  23.     </application>  
  24. </manifest>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值