设为首页 加入收藏

TOP

Android 实用代码片段(三)
2017-10-12 11:37:05 】 浏览:8604
Tags:Android 实用 代码 片段
== 0) { return (T[]) Array.newInstance(cls, 0); } return items.toArray((T[]) Array.newInstance(cls, items.size())); }

17.保存恢复ListView当前位置(可以保存在Preference中或者是数据库中,数据加载完后再设置

 private void saveCurrentPosition() {
        if (mListView != null) {
            int position = mListView.getFirstVisiblePosition();
            View v = mListView.getChildAt(0);
            int top = (v == null) ? 0 : v.getTop();
            //保存position和top
        }
    }
    
    private void restorePosition() {
        if (mFolder != null && mListView != null) {
            int position = 0;//取出保存的数据
            int top = 0;//取出保存的数据
            mListView.setSelectionFromTop(position, top);
        }
    }

18.调用便携式热点和数据共享设置

 public static Intent getHotspotSetting() {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_MAIN);
        ComponentName com = new ComponentName("com.android.settings", "com.android.settings.TetherSettings");
        intent.setComponent(com);
        return intent;
    }

 

19.格式化输出IP地址

public static String getIp(Context ctx) {
        return Formatter.formatIpAddress((WifiManager) ctx.getSystemService(Context.WIFI_SERVICE).getConnectionInfo().getIpAddress());
    }

20.文件夹排序(先文件夹排序,后文件排序)

public static void sortFiles(File[] files) {
        Arrays.sort(files, new Comparator<File>() {

            @Override
            public int compare(File lhs, File rhs) {
                //返回负数表示o1 小于o2,返回0 表示o1和o2相等,返回正数表示o1大于o2。 
                boolean l1 = lhs.isDirectory();
                boolean l2 = rhs.isDirectory();
                if (l1 && !l2)
                    return -1;
                else if (!l1 && l2)
                    return 1;
                else {
                    return lhs.getName().compareTo(rhs.getName());
                }
            }
        });
    }

21.发送不重复的通知Notification(关键点在这个requestCode,这里使用的是当前系统时间,巧妙的保证了每次都是一个新的Notification产生

public static void sendNotification(Context context, String title,
            String message, Bundle extras) {
        Intent mIntent = new Intent(context, FragmentTabsActivity.class);
        mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        mIntent.putExtras(extras);

        int requestCode = (int) System.currentTimeMillis();

        PendingIntent mContentIntent = PendingIntent.getActivity(context,
                requestCode, mIntent, 0);

        Notification mNotification = new NotificationCompat.Builder(context)
                .setContentTitle(title).setSmallIcon(R.drawable.app_icon)
                .setContentIntent(mContentIntent).setContentText(message)
                .build();
        mNotification.flags |= Notification.FLAG_AUTO_CANCEL;
        mNotification.defaults = Notification.DEFAULT_ALL;

        NotificationManager mNotificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);

        mNotificationManager.notify(requestCode, mNotification);
    }

22.代码设置TextView的样式

new TextView(new ContextThemeWrapper(this, R.style.text_style))

23.ip地址转成8位十六进制串

 /** ip转16进制 */
    public static String ipToHex(String ips) {
        StringBuffer result = new StringBuffer();
        if (ips != null) {
            StringTokenizer st = new StringTokenizer(ips, ".");
            while (st.hasMoreTokens()) {
                String token = Integer.toHexString(Integer.parseInt(st.nextToken()));
                if (token.length() == 1)
                    token = "0" + token;
                result.append(token);
            }
        }
        return result.toString();
    }

    /** 16进制转ip */
    public static String texToIp(String ips) {
        try {
            StringBuffer result = new StringBuffer();
            if (ips != null && ips.length() == 8) {
                for (int i = 0; i < 8; i += 2) {
首页 上一页 1 2 3 4 5 6 下一页 尾页 3/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇【Android】3.0 第3章 百度地图及.. 下一篇Android Studio第一次提交git使用

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目