长按home键显示当前任务
公司给我的任务就是怎么长按home键显示当前任务,刚开始我还以为利用一个activity开启一个service就行啦,然后在监听home键,但是监听到了home键,也是不能在service中显示一个view的,view是建立activity的基础上才能显示的,所以必须在一个activity中监听到home键的长按才行,所以最终只能在Launcher中监听home键的长按才行,那怎么监听home键的长按呢,其实我现在想到了两种办法,第一种是监听logcat消息日志,因为每次按下home键都会发送一个系统日志;第二种方法就是在Launcher中监听home键,方法在我的日志中。接着就是要重写一个dialog按钮,然后在里面显示最近用的应用程序,dialog的代码如下:
public class RecentApplicationsDialog extends Dialog implements OnClickListener { // Elements for debugging support private static final String LOG_TAG = "RecentApplicationsDialog"; private static final boolean DBG_FORCE_EMPTY_LIST = false; private static final int NUM_BUTTONS = 8; private static final int MAX_RECENT_TASKS = NUM_BUTTONS * 2; // allow for some discards private int mIconSize; //public static boolean RecentDialog_First = false ;// ratation flag final TextView[] mIcons = new TextView[NUM_BUTTONS]; View mNoAppsText; IntentFilter mBroadcastIntentFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); Handler mHandler = new Handler(); Runnable mCleanup = new Runnable() { public void run() { // dump extra memory we're hanging on to for (TextView icon: mIcons) { icon.setCompoundDrawables(null, null, null, null); icon.setTag(null); } } }; public RecentApplicationsDialog(Context context) { super(context, R.style.RecentApplicationsDialog); final Resources resources = context.getResources(); mIconSize = (int) resources.getDimension(R.dimen.app_icon_size); } /** * We create the recent applications dialog just once, and it stays around (hidden) * until activated by the user. * * @see PhoneWindowManager#showRecentAppsDialog */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Context context = getContext(); Window window = getWindow(); window.requestFeature(Window.FEATURE_NO_TITLE); //window.setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG); window.setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM, WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);//导致界面里面所有需要弹出软键盘的控件均无法显示软键盘 //window.setTitle("Recents"); setContentView(R.layout.recent_apps_dialog); final WindowManager.LayoutParams params = window.getAttributes(); params.dimAmount = 0.5f;//设置透明度(范围是0~1.0f) DisplayMetrics display = new DisplayMetrics(); window.getWindowManager().getDefaultDisplay().getMetrics(display);WindowManager wm = (WindowManager)context.getSystemService(context.WINDOW_SERVICE);Display display_rotation = wm.getDefaultDisplay();params.width = WindowManager.LayoutParams.MATCH_PARENT; params.height = WindowManager.LayoutParams.MATCH_PARENT; window.setAttributes(params); window.setFlags(0, WindowManager.LayoutParams.FLAG_DIM_BEHIND);//遮罩式(模式)顶级窗口--该顶级view下的部件被遮住,不可以操作 mIcons[0] = (TextView)findViewById(R.id.button01); mIcons[1] = (TextView)findViewById(R.id.button02); mIcons[2] = (TextView)findViewById(R.id.button03); mIcons[3] = (TextView)findViewById(R.id.button04); mIcons[4] = (TextView)findViewById(R.id.button05); mIcons[5] = (TextView)findViewById(R.id.button06); mIcons[6] = (TextView)findViewById(R.id.button07); mIcons[7] = (TextView)findViewById(R.id.button08); mNoAppsText = findViewById(R.id.no_applications_message); for (TextView b: mIcons) { b.setOnClickListener(this); } } /** * Handler for user clicks. If a button was clicked, launch the corresponding activity. */ public void onClick(View v) { for (TextView b: mIcons) { if (b == v) { // prepare a launch intent and send it Intent intent = (Intent)b.getTag(); if (intent != null) { intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY); try { getContext().startActivity(intent); } catch (ActivityNotFoundException e) { Log.w("Recent", "Unable to launch recent task", e); } } break; } } dismiss(); } /** * Set up and show the recent activities dialog. */ @Override public void onStart() { super.onStart(); reloadButtons(); //注册广播 getContext().registerReceiver(mBroadcastReceiver, mBroadcastIntentFilter); //Activity不进行icons初始化工作 mHandler.removeCallbacks(mCleanup); } /** * Dismiss the recent activities dialog. */ @Override public void onStop() { super.onStop(); // stop receiving broadcasts getContext().unregisterReceiver(mBroadcastReceiver); mHandler.postDelayed(mCleanup, 100); } /** * Reload the 6 buttons with recent activities */ private void reloadButtons() { final Context context = getContext(); final PackageManager pm = context.getPackageManager(); final ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); final List<ActivityManager.RecentTaskInfo> recentTasks = am.getRecentTasks(MAX_RECENT_TASKS, ActivityManager.RECENT_WITH_EXCLUDED); ActivityInfo homeInfo = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME) .resolveActivityInfo(pm, 0); IconUtilities iconUtilities = new IconUtilities(getContext()); // Performance note: Our android performance guide says to prefer Iterator when // using a List class, but because we know that getRecentTasks() always returns // an ArrayList<>, we'll use a simple index instead. int index = 0; int numTasks = recentTasks.size(); for (int i = 0; i < numTasks && (index < NUM_BUTTONS); ++i) { final ActivityManager.RecentTaskInfo info = recentTasks.get(i); // for debug purposes only, disallow first result to create empty lists if (DBG_FORCE_EMPTY_LIST && (i == 0)) continue; Intent intent = new Intent(info.baseIntent); if (info.origActivity != null) { intent.setComponent(info.origActivity); } // Skip the current home activity. if (homeInfo != null) { if (homeInfo.packageName.equals( intent.getComponent().getPackageName()) && homeInfo.name.equals( intent.getComponent().getClassName())) { continue; } } intent.setFlags((intent.getFlags()&~Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) | Intent.FLAG_ACTIVITY_NEW_TASK); final ResolveInfo resolveInfo = pm.resolveActivity(intent, 0); if (resolveInfo != null) { final ActivityInfo activityInfo = resolveInfo.activityInfo; final String title = activityInfo.loadLabel(pm).toString(); Drawable icon = activityInfo.loadIcon(pm); if (title != null && title.length() > 0 && icon != null) { final TextView tv = mIcons[index]; tv.setText(title); icon = iconUtilities.createIconDrawable(icon); tv.setCompoundDrawables(null, icon, null, null); tv.setTag(intent); tv.setVisibility(View.VISIBLE); tv.setPressed(false); tv.clearFocus(); ++index; } } } // handle the case of "no icons to show" mNoAppsText.setVisibility((index == 0) ? View.VISIBLE : View.GONE); // hide the rest for (; index < NUM_BUTTONS; ++index) { mIcons[index].setVisibility(View.GONE); } } /** * This is the listener for the ACTION_CLOSE_SYSTEM_DIALOGS intent. It's an indication that * we should close ourselves immediately, in order to allow a higher-priority UI to take over * (e.g. phone call received). */ private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) { dismiss(); } }?
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.flyaudio.andriod" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <!-- 获取任务权限 --><uses-permission android:name="android.permission.GET_TASKS"/><!-- 加权限限制Home键 --> <uses-permission android:name="android.permission.DISABLE_KEYGUARD"/> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.HOME" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.MONKEY"/> </intent-filter> </activity> <service android:name=".HomeService"></service> </application></manifest>?
?