1 调用浏览器 载入某网址

view plain copy to clipboard print ?
1.Uri uri = Uri.parse( "http://www.baidu.com" );
2.Intent it = new Intent(Intent.ACTION_VIEW, uri); 3.startActivity(it);
Uri uri = Uri.parse("http://www.baidu.com"); Intent it = new Intent(Intent.ACTION_VIEW, uri); startActivity(it);



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

view plain copy to clipboard print ?
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.}
public class getBroadcast extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if(Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())){ Toast.makeText(context, "有应用被添加", Toast.LENGTH_LONG).show(); } else if(Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())){ Toast.makeText(context, "有应用被删除", Toast.LENGTH_LONG).show(); } else if(Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())){ Toast.makeText(context, "有应用被替换", Toast.LENGTH_LONG).show(); } else if(Intent.ACTION_CAMERA_BUTTON.equals(intent.getAction())){ Toast.makeText(context, "按键", Toast.LENGTH_LONG).show(); } } }

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


view plain copy to clipboard print ?
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>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="zy.Broadcast" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Broadcast" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name="getBroadcast" android:enabled="true" > <intent-filter> <action android:name="android.intent.action.PACKAGE_ADDED"></action> <!-- <action android:name="android.intent.action.PACKAGE_CHANGED"></action>--> <action android:name="android.intent.action.PACKAGE_REMOVED"></action> <action android:name="android.intent.action.PACKAGE_REPLACED"></action> <!-- <action android:name="android.intent.action.PACKAGE_RESTARTED"></action>--> <!-- <action android:name="android.intent.action.PACKAGE_INSTALL"></action>--> <action android:name="android.intent.action.CAMERA_BUTTON"></action> <data android:scheme="package"></data> </intent-filter> </receiver> </application> <uses-sdk android:minSdkVersion="3" /> </manifest>



3 使用Toast输出一个字符串

view plain copy to clipboard print ?
1.public void DisplayToast(String str)
2. {
3. Toast.makeText( this ,str,Toast.LENGTH_SHORT).show(); 4. }
public void DisplayToast(String str) { Toast.makeText(this,str,Toast.LENGTH_SHORT).show(); }



4 把一个字符串写进文件

view plain copy to clipboard print ?
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. }
public void writefile(String str,String path ) { File file; FileOutputStream out; try { //创建文件 file = new File(path); file.createNewFile(); //打开文件file的OutputStream out = new FileOutputStream(file); String infoToWrite = str; //将字符串转换成byte数组写入文件 out.write(infoToWrite.getBytes()); //关闭文件file的OutputStream out.close(); } catch (IOException e) { //将出错信息打印到Logcat DisplayToast(e.toString()); } }



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

view plain copy to clipboard print ?
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. }
public String getinfo(String path) { File file; String str=""; FileInputStream in; try{ //打开文件file的InputStream file = new File(path); in = new FileInputStream(file); //将文件内容全部读入到byte数组 int length = (int)file.length(); byte[] temp = new byte[length]; in.read(temp, 0, length); //将byte数组用UTF-8编码并存入display字符串中 str = EncodingUtils.getString(temp,TEXT_ENCODING); //关闭文件file的InputStream in.close(); } catch (IOException e) { DisplayToast(e.toString()); } return str; }



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

view plain copy to clipboard print ?
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); //正常卸载程序 Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(new File("/sdcard/WorldCupTimer.apk")), "application/vnd.android.package-archive"); startActivity(intent); //安装 程序 Uri packageURI = Uri.parse("package:zy.dnh"); Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI); startActivity(uninstallIntent);//正常卸载程序

7 结束某个进程

view plain copy to clipboard print ?
1.activityManager.restartPackage(packageName);
activityManager.restartPackage(packageName);



8 设置默认来电铃声





view plain copy to clipboard print ?
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. ;}
public void setMyRingtone() { File k = new File("/sdcard/Shall We Talk.mp3"); // 设置歌曲路径 ContentValues values = new ContentValues(); values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath()); values.put(MediaStore.MediaColumns.TITLE, "Shall We Talk"); values.put(MediaStore.MediaColumns.SIZE, 8474325); values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3"); values.put(MediaStore.Audio.Media.ARTIST, "Madonna"); values.put(MediaStore.Audio.Media.DURATION, 230); values.put(MediaStore.Audio.Media.IS_RINGTONE, true); values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false); values.put(MediaStore.Audio.Media.IS_ALARM, false); values.put(MediaStore.Audio.Media.IS_MUSIC, false); // Insert it into the database Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()); Uri newUri = this.getContentResolver().insert(uri, values); RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, newUri); ;}



需要的权限


view plain copy to clipboard print ?
1.<uses-permission android:name= "android.permission.WRITE_SETTINGS" ></uses-permission>
<uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>



模拟HOME按键


view plain copy to clipboard print ?
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);
Intent i=new Intent(Intent.ACTION_MAIN); i.addCategory(Intent.CATEGORY_HOME); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i);



9 打开某一个联系人

view plain copy to clipboard print ?
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);
Intent intent=new Intent(); String data = "content://contacts/people/1"; Uri uri = Uri.parse(data); intent.setAction(Intent.ACTION_VIEW); intent.setData(uri); startActivity(intent);


10 发送文件


view plain copy to clipboard print ?
1.void sendFile(String path)
2.{
3. File mZipFile= new File(path); 4. Intent intent = new Intent( 5. Intent.ACTION_SEND);
6. // intent.setClassName("com.android.bluetooth", "com.broadcom.bt.app.opp.OppLauncherActivity"); 7. // intent.setClassName("com.android.bluetooth", "com.android.bluetooth.opp.BluetoothOppLauncherActivity"); 8. intent.putExtra( "subject" , mZipFile 9. .getName()); // 10.
11. intent.putExtra( "body" , "content by chopsticks" ); // 正文 12. intent.putExtra(Intent.EXTRA_STREAM,
13. Uri.fromFile(mZipFile)); // 添加附件,附件为file对象 14. if (mZipFile.getName().endsWith( ".gz" )) { 15. intent
16. .setType( "application/x-gzip" ); // 如果是gz使用gzip的mime 17. } else if (mZipFile.getName().endsWith( 18. ".txt" )) { 19. intent.setType( "text/plain" ); // 纯文本则用text/plain的mime 20. } else if (mZipFile.getName().endsWith( 21. ".zip" )) { 22. intent.setType( "application/zip" ); // 纯文本则用text/plain的mime 23. } else { 24. intent
25. .setType( "application/octet-stream" ); // 其他的均使用流当做二进制数据来发送 26. }
27. // startActivity(intent); 28. startActivity(
29. Intent.createChooser(intent,
30. "选择蓝牙客户端" )); 31.}


友情链接:珀莱雅 欧诗漫 专卖 http://store.taobao.com/shop/view_shop.htm?mytmenu=mdianpu&utkn=g,2djlrizuga4a1324992712104&user_number_id=372143050

更多相关文章

  1. android 字符串数组资源
  2. mac android(android studio)环境搭建配置详解
  3. android 录音 mediaRecorder
  4. android 资源res下目录使用
  5. Android的数据存储方式
  6. Cocos2d-x 项目从VS移植到Android中的配置
  7. layer-list使用
  8. 箭头函数的基础使用
  9. NPM 和webpack 的基础使用

随机推荐

  1. 十六周总结报告
  2. Android布局优化(二)优雅获取界面布局耗时
  3. RelativeLayout布局的对齐属性
  4. Android占领2010的六大理由!
  5. Android退出当前应用程序的方法
  6. Android JNI打印c\c++日志信息
  7. Android 应用界面设计
  8. 【Android开发学习25】界面布局之相对布
  9. Android 下的Search
  10. 安卓APP开发中的屏幕适配问题如何解决?