Android 常用代码集合

 

1 调用浏览器 载入某网址

  1. Uri uri = Uri.parse("http://www.baidu.com");          
  2. Intent it = new Intent(Intent.ACTION_VIEW, uri);          
  3. startActivity(it);  
 

 

2 Broadcast接收系统广播的intent 监控应用程序包的安装 删除

  1. public class getBroadcast extends BroadcastReceiver {  
  2.         @Override  
  3.         public void onReceive(Context context, Intent intent) {  
  4.                  
  5.                   if(Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())){  
  6.                     Toast.makeText(context, "有应用被添加", Toast.LENGTH_LONG).show();  
  7.             }  
  8.                 else  if(Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())){  
  9.                     Toast.makeText(context, "有应用被删除", Toast.LENGTH_LONG).show();  
  10.             }  
  11.                
  12.                 else  if(Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())){  
  13.                     Toast.makeText(context, "有应用被替换", Toast.LENGTH_LONG).show();  
  14.             }  
  15.                     
  16.                 else  if(Intent.ACTION_CAMERA_BUTTON.equals(intent.getAction())){  
  17.                     Toast.makeText(context, "按键", Toast.LENGTH_LONG).show();  
  18.             }  
  19.               
  20.         }  
  21.          
  22. }  
 

需要声明的权限如下AndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="zy.Broadcast"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  7.         <activity android:name=".Broadcast"  
  8.                   android:label="@string/app_name">  
  9.             <intent-filter>  
  10.                 <action android:name="android.intent.action.MAIN" />  
  11.                 <category android:name="android.intent.category.LAUNCHER" />  
  12.             </intent-filter>  
  13.         </activity>  
  14.       <receiver android:name="getBroadcast" android:enabled="true" >  
  15.          <intent-filter>  
  16.              <action android:name="android.intent.action.PACKAGE_ADDED"></action>  
  17.              <!-- <action android:name="android.intent.action.PACKAGE_CHANGED"></action>-->  
  18.              <action android:name="android.intent.action.PACKAGE_REMOVED"></action>  
  19.              <action android:name="android.intent.action.PACKAGE_REPLACED"></action>  
  20.              <!-- <action android:name="android.intent.action.PACKAGE_RESTARTED"></action>-->  
  21.            <!--    <action android:name="android.intent.action.PACKAGE_INSTALL"></action>-->  
  22.                <action android:name="android.intent.action.CAMERA_BUTTON"></action>  
  23.                <data android:scheme="package"></data>  
  24.               </intent-filter>  
  25. </receiver>  
  26.     </application>  
  27.     <uses-sdk android:minSdkVersion="3" />  
  28.       
  29. </manifest>   
 

 

3 使用Toast输出一个字符串

  1. public void DisplayToast(String str)  
  2.         {  
  3.       Toast.makeText(this,str,Toast.LENGTH_SHORT).show();  
  4.         }   
 

 

4 把一个字符串写进文件

  1. public void writefile(String str,String path )  
  2.         {  
  3.             File file;  
  4.             FileOutputStream out;  
  5.              try {  
  6.                  //创建文件   
  7.                  file = new File(path);  
  8.                  file.createNewFile();  
  9.                    
  10.                  //打开文件file的OutputStream   
  11.                  out = new FileOutputStream(file);  
  12.                  String infoToWrite = str;  
  13.                  //将字符串转换成byte数组写入文件   
  14.                  out.write(infoToWrite.getBytes());  
  15.                  //关闭文件file的OutputStream   
  16.                  out.close();  
  17.                    
  18.                    
  19.                    
  20.              } catch (IOException e) {  
  21.                  //将出错信息打印到Logcat   
  22.               DisplayToast(e.toString());  
  23.                    
  24.              }  
  25.         }  
 

 

5 把文件内容读出到一个字符串

  1. public String getinfo(String path)  
  2.         {  
  3.             File file;  
  4.             String str="";   
  5.             FileInputStream in;  
  6.          try{  
  7.             //打开文件file的InputStream   
  8.              file = new File(path);  
  9.              in = new FileInputStream(file);  
  10.              //将文件内容全部读入到byte数组   
  11.              int length = (int)file.length();  
  12.              byte[] temp = new byte[length];  
  13.              in.read(temp, 0, length);  
  14.              //将byte数组用UTF-8编码并存入display字符串中   
  15.              str =  EncodingUtils.getString(temp,TEXT_ENCODING);  
  16.              //关闭文件file的InputStream   
  17.                
  18.              in.close();  
  19.          }  
  20.          catch (IOException e) {  
  21.                
  22.           DisplayToast(e.toString());  
  23.                
  24.          }  
  25.          return str;  
  26.         }  
 

 

6 调用Android installer 安装和卸载程序

  1. Intent intent = new Intent(Intent.ACTION_VIEW);   
  2.        intent.setDataAndType(Uri.fromFile(new File("/sdcard/WorldCupTimer.apk")), "application/vnd.android.package-archive");   
  3.        startActivity(intent); //安装 程序   
  4.          
  5.        Uri packageURI = Uri.parse("package:zy.dnh");       
  6.        Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);       
  7.        startActivity(uninstallIntent);//正常卸载程序  
 

7 结束某个进程

  1. activityManager.restartPackage(packageName);  
 

 

8 设置默认来电铃声

 

 

  1. public void setMyRingtone()  
  2.     {  
  3.    File k = new File("/sdcard/Shall We Talk.mp3"); // 设置歌曲路径   
  4.     ContentValues values = new ContentValues();  
  5.     values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());  
  6.     values.put(MediaStore.MediaColumns.TITLE, "Shall We Talk");  
  7.     values.put(MediaStore.MediaColumns.SIZE, 8474325);  
  8.     values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");  
  9.     values.put(MediaStore.Audio.Media.ARTIST, "Madonna");  
  10.     values.put(MediaStore.Audio.Media.DURATION, 230);  
  11.     values.put(MediaStore.Audio.Media.IS_RINGTONE, true);  
  12.     values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);  
  13.     values.put(MediaStore.Audio.Media.IS_ALARM, false);  
  14.     values.put(MediaStore.Audio.Media.IS_MUSIC, false);  
  15.     // Insert it into the database   
  16.     Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());  
  17.     Uri newUri = this.getContentResolver().insert(uri, values);  
  18.     RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, newUri);  
  19.     ;}  
 

 

需要的权限

  1. <uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>  
 

 

模拟HOME按键

  1. Intent i=new Intent(Intent.ACTION_MAIN);  
  2. i.addCategory(Intent.CATEGORY_HOME);  
  3. i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  4. context.startActivity(i);  
 

 

9 打开某一个联系人

  1. Intent intent=new Intent();  
  2. String data = "content://contacts/people/1";  
  3. Uri  uri = Uri.parse(data);          intent.setAction(Intent.ACTION_VIEW);  
  4. intent.setData(uri);  
  5. startActivity(intent);  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值