開發一個android案頭

來源:互聯網
上載者:User

標籤:網路   category   ima   add   imageview   params   scaletype   icon   util   

本文的原文串連是: http://blog.csdn.net/freewebsys/article/details/53363731 未經博主允許不得轉載。
博主地址是:http://blog.csdn.net/freewebsys

1,關於lancher

要開發一個自己的案頭,作為程式員,覺得好多的應用都在偷偷的跑流量。
開發一個傳統型程式,然後常駐系統服務,定時檢查服務。
對系統的其他應用網路進行監控。把一些常用的功能整合進來。
做一個訊飛的Voice Messaging Service。實現一些簡單的人機互動。
參考一個哥們寫的:
http://blog.csdn.net/sljjyy/article/details/11927713

2,修改設定檔

在AndroidManifest.xml 設定檔中增加兩行即可。

        <activity android:name=".MainActivity">            <intent-filter>                <!-- 增加lancher配置-->                <category android:name="android.intent.category.HOME" />                <category android:name="android.intent.category.DEFAULT" />                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>

然後這個預設的activity程式就成案頭了。按home的時候。
會彈出一個選擇框選擇案頭。

3,進行最佳化

查詢全部app

Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);        new ImageView(MainActivity.this);        apps = getPackageManager().queryIntentActivities(mainIntent, 0);        if (apps != null) {            for (ResolveInfo resolveInfo : apps) {                Log.v(TAG, resolveInfo.toString());            }        }

把返回的app顯示到主介面中。
實現一個BaseAdapter 類。實現getView 方法。

ResolveInfo info = apps.get(i);            View convertView = LayoutInflater.from(mContent).inflate(R.layout.text_img_view, null);            ImageView image = (ImageView) convertView.findViewById(R.id.image);            TextView text = (TextView) convertView.findViewById(R.id.text);            //設定文字和圖片。            text.setText(info.loadLabel(getPackageManager()));            image.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));            // convertView.setScaleType(ImageView.ScaleType.FIT_CENTER);            //使用dp進行參數設定。進行解析度適配。            convertView.setLayoutParams(new GridView.LayoutParams(                    (int) mResources.getDimension(R.dimen.app_width),                    (int) mResources.getDimension(R.dimen.app_height)));            //返回一個圖文混合。            return convertView;

定義了一個layout

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <ImageView        android:id="@+id/image"        android:layout_width="50dp"        android:layout_height="50dp"        android:layout_gravity="center_horizontal" />    <TextView        android:id="@+id/text"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center" /></LinearLayout>

上面是圖片,下面是文字。
還定義了寬度和高度

<resources>    <dimen name="app_width">90dp</dimen>    <dimen name="app_height">90dp</dimen></resources>

運行效果:

很接近了。
最後在設定下開啟。壁紙啥的。就都一樣了。

4,全部代碼
package com.demo.lanchertest;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.pm.ResolveInfo;import android.content.res.Resources;import android.net.Uri;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.AbsListView;import android.widget.AdapterView;import android.widget.BaseAdapter;import android.widget.GridView;import android.widget.ImageView;import android.widget.TextView;import com.google.android.gms.appindexing.Action;import com.google.android.gms.appindexing.AppIndex;import com.google.android.gms.appindexing.Thing;import com.google.android.gms.common.api.GoogleApiClient;import java.security.AccessControlContext;import java.util.List;import static java.security.AccessController.getContext;public class MainActivity extends AppCompatActivity {    public static final String TAG = "Lancher";    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //載入app應用。        loadApps();        GridView gridView = (GridView) findViewById(R.id.apps_list);        //設定預設適配器。        mContent = getApplicationContext();        mResources = getResources();        gridView.setAdapter(new AppsAdapter());        //        gridView.setOnItemClickListener(clickListener);    }    private AdapterView.OnItemClickListener clickListener = new AdapterView.OnItemClickListener() {        @Override        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {            ResolveInfo info = apps.get(i);            //該應用的包名            String pkg = info.activityInfo.packageName;            //應用的主activity類            String cls = info.activityInfo.name;            ComponentName componet = new ComponentName(pkg, cls);            Intent intent = new Intent();            intent.setComponent(componet);            startActivity(intent);        }    };    private List<ResolveInfo> apps;    private Resources mResources;    private Context mContent;    private void loadApps() {        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);        new ImageView(MainActivity.this);        apps = getPackageManager().queryIntentActivities(mainIntent, 0);        if (apps != null) {            for (ResolveInfo resolveInfo : apps) {                Log.v(TAG, resolveInfo.toString());            }        }    }    public class AppsAdapter extends BaseAdapter {        public AppsAdapter() {        }        @Override        public int getCount() {            return apps.size();        }        @Override        public Object getItem(int i) {            return apps.get(i);        }        @Override        public long getItemId(int i) {            return i;        }        @Override        public View getView(int i, View view, ViewGroup viewGroup) {            ResolveInfo info = apps.get(i);            View convertView = LayoutInflater.from(mContent).inflate(R.layout.text_img_view, null);            ImageView image = (ImageView) convertView.findViewById(R.id.image);            TextView text = (TextView) convertView.findViewById(R.id.text);            //設定文字和圖片。            text.setText(info.loadLabel(getPackageManager()));            image.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));            // convertView.setScaleType(ImageView.ScaleType.FIT_CENTER);            //使用dp進行參數設定。進行解析度適配。            convertView.setLayoutParams(new GridView.LayoutParams(                    (int) mResources.getDimension(R.dimen.app_width),                    (int) mResources.getDimension(R.dimen.app_height)));            //返回一個圖文混合。            return convertView;        }    }}

全部代碼不過130 行。但是很有意思。

5,總結

本文的原文串連是: http://blog.csdn.net/freewebsys/article/details/53363731 未經博主允許不得轉載。
博主地址是:http://blog.csdn.net/freewebsys

開發一個自訂案頭還是挺有意思的。
想咋控制就咋控制呢。一頁顯示多少app,文字大小,表徵圖。

開發一個android案頭

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.