这篇文章主要记录一些常用的一些代码段,方便以后查阅,不断更新中。
1:调用浏览器,载入某网址
1
2
3
|
Intent new Intent(Intent.ACTION_VIEW, startActivity(it); |
2:Broadcast接收系统广播的intent监控应用程序包的安装、删除
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public class getBroadcast extends BroadcastReceiver @Override public void onReceive(Context if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())) Toast.makeText(context, "有应用被添加" , } else if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) Toast.makeText(context, "有应用被删除" , } else if (Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())) Toast.makeText(context, "有应用被替换" , } else if (Intent.ACTION_CAMERA_BUTTON.equals(intent.getAction())) Toast.makeText(context, "按键" , } } } |
需要声明的权限如下AndroidManifest.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<? xml version = "1.0" encoding = "utf-8" ?> 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_REMOVED" ></ action > < action android:name = "android.intent.action.PACKAGE_REPLACED" ></ 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输出一个字符串
1
2
3
|
public void DisplayToast(String Toast.makeText( this ,str,Toast.LENGTH_SHORT).show(); } |
4:把一个字符串写进文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
public void writefile(String File FileOutputStream try { // file new File(path); file.createNewFile(); //打开文件file的OutputStream out new FileOutputStream(file); String //将字符串转换成byte数组写入文件 out.write(infoToWrite.getBytes()); //关闭文件file的OutputStream out.close(); } catch (IOException // DisplayToast(e.toString()); } } |
5:把文件内容读出到一个字符串
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
public String File String "" ; FileInputStream try { //打开文件file的InputStream file new File(path); in new FileInputStream(file); //将文件内容全部读入到byte数组 int length int )file.length(); byte [] new byte [length]; in.read(temp, 0 , //将byte数组用UTF-8编码并存入display字符串中 str //关闭文件file的InputStream in.close(); } catch (IOException DisplayToast(e.toString()); } return str; } |
6:调用Android installer安装和卸载程序
1
2
3
4
5
6
7
|
Intent new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile( new File( "/sdcard/WorldCupTimer.apk" )), "application/vnd.android.package-archive" ); startActivity(intent); //安装 Uri "package:zy.dnh" ); Intent new Intent(Intent.ACTION_DELETE, startActivity(uninstallIntent); //正常卸载程序 |
7:结束某个进程
1
|
activityManager.restartPackage(packageName); |
8:设置默认来电铃声
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public void setMyRingtone() File new File( "/sdcard/Shall ); // ContentValues new ContentValues(); values.put(MediaStore.MediaColumns.DATA, values.put(MediaStore.MediaColumns.TITLE, "Shall ); 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 ); // Uri Uri this .getContentResolver().insert(uri, RingtoneManager.setActualDefaultRingtoneUri( this , } |
需要的权限
1
|
< uses-permission android:name = "android.permission.WRITE_SETTINGS" ></ uses-permission > |
模拟HOME按键
1
2
3
4
|
Intent new Intent(Intent.ACTION_MAIN); i.addCategory(Intent.CATEGORY_HOME); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); |
9:打开某一个联系人
1
2
3
4
5
6
|
Intent new Intent(); Uri intent.setAction(Intent.ACTION_VIEW); intent.setData(uri); startActivity(intent); |
10:发送文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
void sendFile(String File new File(path); Intent new Intent(Intent.ACTION_SEND); // // intent.putExtra( "subject" , .getName()); // intent.putExtra( "body" , "content ); // intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(mZipFile)); // if (mZipFile.getName().endsWith( ".gz" )) intent .setType( "application/x-gzip" ); // } else if (mZipFile.getName().endsWith( ".txt" )) intent.setType( "text/plain" ); // } else if (mZipFile.getName().endsWith( ".zip" )) intent.setType( "application/zip" ); // } else { intent .setType( "application/octet-stream" ); // } // startActivity( Intent.createChooser(intent, "选择蓝牙客户端" )); } |
//安装apk文件
private void installAPK(File file) {
//卸载apk文件
//从SIM卡中获取联系人
private Cursor getContacts() {
Uri uri = Uri.parse("content://sim/adn");
String[] projection = new String[] { "name", "phone" };
String selection = null;
String[] selectionArgs = null;
String sortOrder = null;
return managedQuery(uri, projection, selection, selectionArgs,sortOrder);
}