ホームキーを長く押すと現在のタスクが表示されます

11889 ワード

会社が私に与えた任務はどのようにホームキーを長く押して現在の任務を表示するかであり、最初はactivityを利用してサービスを開ければいいと思っていたが、ホームキーを傍受していたが、ホームキーを傍受しても、サービスにviewを表示することはできなかった.viewはactivityを確立した上で表示できるので、activityでホームキーの長押しを傍受しなければならない.だから最終的にLauncherの中でhomeキーの長い押しを傍受するしかなくて、それではどのようにhomeキーの長い押しを傍受しますか、実は私は今2つの方法を考えて、1つはlogcatメッセージログを傍受して、homeキーを押すたびに1つのシステムログを送信するためです;2つ目の方法は、Launcherでホームキーを傍受することです.方法は私のログにあります.次に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>