zoukankan      html  css  js  c++  java
  • Android自定义简易luancherDemo(有图有真相哦)

    就两个界面,一个是所有程序,一个是开机动画设置,还有设置壁纸。

    比较粗糙,因为项目停滞,所以就没有继续开发,在此分享一下,望高手莫喷。

    有些代码是网上摘抄,找不到出处了,原作者莫怪咯!

    效果图:

    项目结构:

    HomeActivity.java(所有程序界面)

      1 package com.bvin.demo.launcher;
      2 
      3 import java.util.List;
      4 
      5 import android.app.Activity;
      6 import android.content.ComponentName;
      7 import android.content.Intent;
      8 import android.content.pm.ResolveInfo;
      9 import android.os.Bundle;
     10 import android.util.Log;
     11 import android.view.Menu;
     12 import android.view.MenuItem;
     13 import android.view.View;
     14 import android.view.ViewGroup;
     15 import android.widget.AdapterView;
     16 import android.widget.BaseAdapter;
     17 import android.widget.GridView;
     18 import android.widget.ImageView;
     19 
     20 public class HomeActivity extends Activity {
     21     /** Called when the activity is first created. */
     22     
     23     List<ResolveInfo> mApp;
     24     GridView mGrid;
     25     
     26     @Override
     27     public void onCreate(Bundle savedInstanceState) {
     28         super.onCreate(savedInstanceState);
     29         loadApps();
     30         setContentView(R.layout.main);
     31         initsViews();
     32     }
     33     
     34     
     35     
     36     @Override
     37     public boolean onCreateOptionsMenu(Menu menu) {
     38         // TODO Auto-generated method stub
     39         menu.add(0, 0, 0, "设置壁纸");
     40         menu.add(0, 1, 1, "开机动画");
     41         return super.onCreateOptionsMenu(menu);
     42     }
     43 
     44 
     45 
     46     @Override
     47     public boolean onOptionsItemSelected(MenuItem item) {
     48         // TODO Auto-generated method stub
     49         switch(item.getItemId()){
     50         
     51         case 0:
     52             onSetWallPaper();
     53             break;
     54         case 1:
     55             Intent intent = new Intent(HomeActivity.this,SetActivity.class);
     56             startActivity(intent);
     57             break;
     58         
     59         }
     60         
     61         return super.onOptionsItemSelected(item);
     62     }
     63 
     64 
     65 
     66     void loadApps(){
     67         Intent mainIntent = new Intent(Intent.ACTION_MAIN,null);
     68         mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
     69         mApp = this.getPackageManager().queryIntentActivities(mainIntent, 0);
     70         Log.e("mApp", ""+mApp.size());
     71     }
     72     
     73     void initsViews(){
     74         mGrid = (GridView)findViewById(R.id.appGrid);
     75         mGrid.setAdapter(new AppsAdapter());
     76         
     77         mGrid.setOnItemClickListener(new GridView.OnItemClickListener(){
     78 
     79             @Override
     80             public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
     81                     long arg3) {
     82                 // TODO Auto-generated method stub
     83                 ResolveInfo info = mApp.get(arg2);
     84                 String pkg = info.activityInfo.packageName;
     85                 String cls = info.activityInfo.name;
     86                 ComponentName compoment = new ComponentName(pkg,cls);
     87                 Intent i = new Intent();
     88                 i.setComponent(compoment);
     89                 startActivity(i);
     90             }
     91             
     92         });
     93     }
     94     
     95     void onSetWallPaper(){
     96         
     97         Intent pickWallPager = new Intent(Intent.ACTION_SET_WALLPAPER);
     98         Intent chooser = Intent.createChooser(pickWallPager, "chooser_wallpaper");
     99         startActivity(chooser);
    100     }
    101     
    102     class AppsAdapter extends BaseAdapter{
    103 
    104         @Override
    105         public int getCount() {
    106             // TODO Auto-generated method stub
    107             return mApp.size();
    108         }
    109 
    110         @Override
    111         public Object getItem(int position) {
    112             // TODO Auto-generated method stub
    113             return mApp.get(position);
    114         }
    115 
    116         @Override
    117         public long getItemId(int position) {
    118             // TODO Auto-generated method stub
    119             return position;
    120         }
    121 
    122         @Override
    123         public View getView(int position, View convertView, ViewGroup parent) {
    124             // TODO Auto-generated method stub
    125             ImageView i;
    126             if(convertView==null){
    127                 i = new ImageView(HomeActivity.this);
    128                 i.setScaleType(ImageView.ScaleType.FIT_CENTER);
    129                 i.setLayoutParams(new GridView.LayoutParams(70, 70));
    130                 
    131             }else{
    132                 i = (ImageView)convertView;
    133             }
    134             ResolveInfo info = mApp.get(position);
    135             i.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));
    136             return i;
    137         }
    138         
    139         
    140     }
    141 }

    SettingActivity(开机动画设置界面)

      1 package com.bvin.demo.launcher;
      2 
      3 import java.io.DataOutputStream;
      4 import java.io.File;
      5 import java.io.FileNotFoundException;
      6 import java.io.FileOutputStream;
      7 import java.io.IOException;
      8 import java.io.InputStream;
      9 import java.io.OutputStream;
     10 
     11 import android.app.Activity;
     12 import android.app.ProgressDialog;
     13 import android.os.Bundle;
     14 import android.util.Log;
     15 import android.view.View;
     16 import android.widget.Button;
     17 import android.widget.Toast;
     18 
     19 public class SetActivity extends Activity{
     20 
     21     Button btDell;
     22     Button byMi;
     23     String apkRoot="chmod 777 /system/media";
     24     @Override
     25     protected void onCreate(Bundle savedInstanceState) {
     26         // TODO Auto-generated method stub
     27         super.onCreate(savedInstanceState);
     28         setContentView(R.layout.setwallpaper_layout);
     29         initViews();
     30     }
     31 
     32     void initViews(){
     33         btDell  = (Button)findViewById(R.id.btDell);
     34         byMi  = (Button)findViewById(R.id.btMis1);
     35         
     36         btDell.setOnClickListener(new View.OnClickListener() {
     37             
     38             @Override
     39             public void onClick(View v) {
     40                 // TODO Auto-generated method stub
     41                 try {
     42                     if(rootConmmand(apkRoot)){
     43                         write2System(R.raw.dell);
     44                         setTitle("当前开机动画为戴尔原生动画");
     45                     }else{
     46                         Toast.makeText(SetActivity.this, "未获取root权限", Toast.LENGTH_SHORT).show();
     47                     }
     48                     
     49                 } catch (FileNotFoundException e) {
     50                     // TODO Auto-generated catch block
     51                     e.printStackTrace();
     52                 }
     53             }
     54         });
     55         byMi.setOnClickListener(new View.OnClickListener() {
     56             
     57             @Override
     58             public void onClick(View v) {
     59                 // TODO Auto-generated method stub
     60                 try {
     61                     if(rootConmmand(apkRoot)){
     62                     rootConmmand("chmod 777 /system/media/bootanimation.zip");
     63                     write2System(R.raw.mi);
     64                     setTitle("当前开机动画为小米开机动画");
     65                     }else{
     66                         Toast.makeText(SetActivity.this, "未获取root权限", Toast.LENGTH_SHORT).show();
     67                     }
     68                 } catch (FileNotFoundException e) {
     69                     // TODO Auto-generated catch block
     70                     e.printStackTrace();
     71                 }
     72             }
     73         });
     74         
     75     }
     76     
     77     public static boolean rootConmmand(String cmd){
     78         
     79         Process process = null;
     80         DataOutputStream  os = null;
     81         
     82         try {
     83             process = Runtime.getRuntime().exec("su");
     84             os = new DataOutputStream(process.getOutputStream());
     85             os.writeBytes(cmd+"\n");
     86             os.writeBytes("exit\n");
     87             os.flush();
     88             process.waitFor();
     89             
     90         } catch (IOException e) {
     91             // TODO Auto-generated catch block
     92             Log.d("*** DEBUG ***", "ROOT REE" + e.getMessage());
     93             e.printStackTrace();
     94         } catch (InterruptedException e) {
     95             // TODO Auto-generated catch block
     96             e.printStackTrace();
     97         }finally{
     98             
     99             try {
    100                 if(os!=null){
    101                     os.close();
    102                 }
    103                 process.destroy();
    104             } catch (IOException e) {
    105                 // TODO Auto-generated catch block
    106                 e.printStackTrace();
    107             }
    108         }
    109         Log.d("*** DEBUG ***", "Root SUC ");
    110         return true;
    111     }
    112     
    113     private void write2System(int src) throws FileNotFoundException{
    114         
    115         ProgressDialog pd = new ProgressDialog(this);
    116         pd.show();
    117         InputStream is = this.getResources().openRawResource(src);
    118         OutputStream os = null;
    119         File file = new File("/system/media","bootanimation.zip");
    120         
    121         os = new FileOutputStream(file);
    122         
    123         
    124         byte[] buf = null;
    125         int length;
    126         try {
    127             buf = new byte[1024];
    128             while((length=is.read(buf))!=-1){
    129                 os.write(buf, 0, length);
    130                 Log.e("写", ""+length);
    131                 
    132             }
    133         } catch (IOException e) {
    134             // TODO Auto-generated catch block
    135             e.printStackTrace();
    136         }finally{
    137             if(is!=null){
    138                 try {
    139                     is.close();
    140                 } catch (IOException e) {
    141                     // TODO Auto-generated catch block
    142                     e.printStackTrace();
    143                 }
    144             }
    145             
    146         }
    147         rootConmmand("chmod 777 /system/media/bootanimation.zip");
    148         Log.e("dfg", "ok");
    149         pd.dismiss();
    150         Toast.makeText(this, "开机动画安装成功,可重启查看效果", Toast.LENGTH_LONG).show();
    151         
    152     }
    153     
    154 }

    没有挂载为读写

    加上mount -rw -o remount /dev/block/mmcblk0p4 /system

  • 相关阅读:
    java语言体系的技术简介之JSP、Servlet、JDBC、JavaBean(Application)
    浅谈HTTP中Get与Post的区别
    浅谈HTTP中Get与Post的区别
    图文混排
    Algorithm: quick sort implemented in python 算法导论 快速排序
    algorithm: heap sort in python 算法导论 堆排序
    Leetcode OJ : Compare Version Numbers Python solution
    Python 同时for遍历多个列表
    Leetcode OJ : Repeated DNA Sequences hash python solution
    Leetcode OJ : Triangle 动态规划 python solution
  • 原文地址:https://www.cnblogs.com/bvin/p/2720486.html
Copyright © 2011-2022 走看看