当前位置:操作系统 > 安卓/Android >>

Android代码片段

判断当前应用是否是最上层的应用:
 
public static boolean isTopApp(Context context) {
        ActivityManager activityManager = (ActivityManager) context
                .getSystemService(ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> tasks = activityManager
                .getRunningTasks(5);
        if (tasks == null || tasks.isEmpty()) {
            return false;
        }
        RunningTaskInfo task = tasks.get(0);
        String taskPackageName = task.topActivity.getPackageName();// task任务的package包名
        System.out.println(taskPackageName);
        // System.out.println(task.topActivity.getClassName());
        // System.out.println(task.baseActivity.getClassName());
        return context.getPackageName().equals(taskPackageName);
    }


本机IP地址:
Enumeration<NetworkInte易做图ce> networkInfo = NetworkInte易做图ce
                        .getNetworkInte易做图ces();
                while (networkInfo.hasMoreElements())
                {
                    NetworkInte易做图ce intf = networkInfo.nextElement();
                    Enumeration<InetAddress> intfAddress = intf
                            .getInetAddresses();
                    while (intfAddress.hasMoreElements())
                    {
                        InetAddress inetAddress = intfAddress.nextElement();
                        if (!inetAddress.isLoopbackAddress())
                        {
                            ip = inetAddress.getHostAddress().toString();
                        }
                    }
                }


屏幕分辨率及其相关属性:
DisplayMetrics dm = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
System.out.println(dm.widthPixels + " - " + dm.heightPixels);

第二种方式获取DipalyMetrics对象(!= 上一种方式获取的对象 )
DisplayMetrics dm = activity.getResources().getDisplayMetrics();

Activity页面切换效果:
new Handler().postDelayed(new Runnable() {
            @Override
            public void run()
            {
                Intent intent = new Intent(TransitionActivity.this, DeviceActivity.class);
                startActivity(intent);
                finish();
                //页面切换 -- 淡入淡出效果
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
//                overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
            }
        }, 5000);

 

摘自  我说_小莫 
补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,