前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >032android初级篇之Timer的使用及获取栈顶包名

032android初级篇之Timer的使用及获取栈顶包名

作者头像
上善若水.夏
发布2018-09-28 10:42:47
6080
发布2018-09-28 10:42:47
举报
文章被收录于专栏:上善若水上善若水

测试代码

这段代码的主要功能是使用Timer 定时更新计数。

代码语言:javascript
复制
public class TestTimerActivity extends Activity{
    private  final static String TAG=TestTimerActivity.class.getSimpleName();

    private Timer mWaitTimer;
    private Handler mHandler;
    private TextView mTextView;
    private int mTimes;

    @Override
    protected void onCreate(Bundle savedInstanceState ) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_timer_activity);
        mTextView = (TextView) findViewById(R.id.textView);
        mTextView.setText("TestTimer!");
        mTimes =0;

        mHandler = new Handler() {

            public  void handleMessage(Message msg){
                switch (msg.what){
                    case 1:
                        mTextView.setText(""+mTimes);

                }
                super.handleMessage(msg);
            }
        };

        TimerTask mTimerTask = new TimerTask(){
            @Override
            public void run()
            {
                mTimes++;
                Message msg = new Message();
                msg.what = 1;
                mHandler.sendMessage(msg);
            }
        };
        mWaitTimer = new Timer(true);
        mWaitTimer.schedule(mTimerTask,1000,1000);

    }
}

获取栈顶信息

android 5.0 之后官方屏蔽了获取栈顶信息的api,如下的方法在大多数机器上可用:

代码语言:javascript
复制
 public String getTaskPackname() {
    ActivityManager.RunningAppProcessInfo currentInfo = null;
    Field field = null;
    int START_TASK_TO_FRONT = 2;
    String currentApp = "CurrentNULL";
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
        try {
            field = ActivityManager.RunningAppProcessInfo.class.getDeclaredField("processState");
        } catch (Exception e) {
            e.printStackTrace();
        }
        ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appList = am.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo app : appList) {
            if (app.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                Integer state = null;
                try {
                    state = field.getInt(app);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (state != null && state == START_TASK_TO_FRONT) {
                    currentInfo = app;
                    break;
                }
            }
        }
        if (currentInfo != null) {
            currentApp = currentInfo.processName;
        }
    } else {
        ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> tasks = am.getRunningAppProcesses();
        currentApp = tasks.get(0).processName;
    }
    // Log.e("TAG", "Current App in foreground is: " + currentApp);
    return currentApp;
}

参考链接

  1. Android 计时器Timer用法
  2. Android 监听网络状态+源代码
  3. Android 5.0(包含5.0以下版本) 获取栈顶应用程序包名
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016.02.23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 测试代码
  • 获取栈顶信息
  • 参考链接
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档