- 浏览: 114745 次
- 性别:
- 来自: 广州
文章分类
最新评论
-
瘾心丶Android:
[color=red][/colo[size=large][/ ...
android捕获Home键的方法 -
j086924:
good share!
android Launcher——ui框架 -
m331006:
a、进入cocos2d-x\testandroid\andro ...
Android cocos2d-x开发(三)之创建Android工程和编译 -
sphway:
为什么在布局文件中加入android:layout_gravi ...
自定义控件---滑动按钮的实现 -
chenshijun0101:
LZ,你能把长按home键显示当前任务实现此功能的源码发给我一 ...
长按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>
评论
1 楼
chenshijun0101
2012-07-30
LZ,你能把长按home键显示当前任务实现此功能的源码发给我一份吗?308069028@qq.com,我现在在搞这个问题,急求,谢谢拉!
发表评论
-
Android Service学习之AIDL, Parcelable和远程服务
2012-06-26 19:33 648AIDL的作用 由于每 ... -
Android的系统的Binder机制(一)
2012-06-21 19:11 603Android系统的Binder机制之一—— ... -
Android的系统的Binder机制(一)
2012-06-21 19:11 682Android系统的Binder机制之一—— ... -
android中修改framework层代码后怎样操作才能看到修改后的效果?
2012-03-30 10:24 40171.下面方法适合真机:下载android源码,然后编译你修改的 ... -
Ubuntu下常用的快捷键
2012-03-19 09:10 0最常用 win+n切换背景颜色风格 alt+tab=不用说了 ... -
Android中全屏无标题设置
2012-03-06 10:29 2346方法一:在java代码中实现 //取消标题 ... -
如何通过wifi调试android程序
2012-03-06 09:53 3295android手机居然可以通过wifi进行程序的调试,太好 ... -
android捕获Home键的方法
2012-03-05 10:08 2367Level5以上(包含)中,Activity类中有如下方法: ... -
在32位Ubuntu10.04上编译Android2.3
2012-02-18 11:23 01.安装JDK6 对于Android2.3系统,不要安 ... -
自定义控件---滑动按钮的实现
2012-02-11 16:27 2565package cn.flyaudio.android; ... -
滑动按钮
2012-02-10 19:09 679http://blog.csdn.net/luoyuhhy/a ... -
Android 支持多屏幕机制
2012-02-10 09:20 648在Android上进行开发, ... -
Android UI开发专题
2012-02-06 09:36 858Android UI开发专题 (一) ... -
Launcher(一)关于使用Eclipse调试Launcher的完美解决方法
2012-02-02 19:21 1234由于在Android源码中,很多方法、成员、类、包都被打上@h ... -
android Launcher——ui框架
2012-01-31 22:25 1056android Launcher——ui框架 分 ... -
为程序员量身定制的12个目标
2012-01-15 15:01 583ugmbbc发布于 2012-01-15 ... -
Ubuntu Linux中连接Android真机调试
2012-01-14 14:50 1164首先确保用数据线链接后能识别设备,输入:lsusb,可以看 ... -
如何成为一名Android开发高手
2012-01-04 16:42 1794下面是成为一名真正的Android高手必须掌握和遵循的一些准则 ... -
android GestureDetector功能和使用
2011-12-26 15:36 836android为了增加用户体验 ... -
Android 系统自带样式Android:theme
2011-12-26 15:23 749•android:theme="@android: ...
相关推荐
此类为安卓源码 ,包括长按home键,电源键,back 键,音量键,等
模拟android长按home键,弹出近期任务。对应的文章为:http://blog.csdn.net/yanbin1079415046/article/details/8026613
关闭长按home呼出语音助手 - 荣耀9,理论其他型号也是可以的 当手机按住HOME键3秒后会出现语音助手,但是有时一点击就是语音助手很烦人,这个功能手机上简单设置无法完全关闭,下次按HOME键时间稍长一点又会提示是否...
单片机c语言按键扫面程序框架,有短按,连续按,长按1,长按2等功能。方便移植
按键消抖、长按、短按处理,包括连续累加累减,按键功能旗标的置起
经典的按键扫描驱动程序 分短按、长按区别
有关单片机的 按键 长按 短按 双击 组合按程序分享啊
按时间大于1s后释放,触发长按事件,串口打印"长按" 本程序只供学习使用,未经作者许可,不得用于其它任何用途 感谢大家的阅读与下载,码字开源分享不易,麻烦给个免费的赞。 如果有帮助的,请不要吝啬三连。点赞...
jQuery长按图片中显示图片代码是一款长按按钮图片画中画显示,适用于手机端展示代码。
JQuery Long Press Picture Code is a long press button Picture Picture in Picture Display, suitable for mobile display code.
用于esp8266的按键驱动,支持短按,长按。通过回调接收按键。另外,可以通过传参设置按键个数,按键相应时间。
今天刚好在PhoneWindowManager.java下看,当看到长按Home键的功能时,突然想到是不是可以长按back键来弹出关机框。 有想法就试试呗。当然想法是根据长按home键来的,那么我们应该可以模仿长按Home键来做。经过一番...
STM32按键(单击、双击、长按)操作
软件介绍 MultiTaskin Pro可以让您在正在运行和近期的任务之间快速切换,相比原生的“长按HOME键调出近期任务”而言,它不仅仅可以切换,还可以管理任务和服务,功能更加强大 主要功能: 支持三种激活方式:单击...
STM32按键功能,判断按键按下多少秒,返回长按与否
android ListView显示数据,长按item显示菜单管理当前的item。
按键读取后,可调用该程序进行软件消抖,同时判断按键是不是长按。不是采用延时检测的方式,不会阻塞程序。
基于STM32+MultiButton实现的按键检测单击双击长按保持按键松开按键弹起等源码+说明文档.zip
NULL 博文链接:https://dingran.iteye.com/blog/1600351