这篇文章主要记录一些常用的一些代码段,方便以后查阅,不断更新中。

1:调用浏览器,载入某网址

1

2

3

Uri uri = Uri.parse("http://www.android-study.com");

Intent it = new Intent(Intent.ACTION_VIEW, uri);

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 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

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

<?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输出一个字符串

?

1

2

3

public void DisplayToast(String str) {

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 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:把文件内容读出到一个字符串

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 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:调用Androidinstaller安装和卸载程序

1

2

3

4

5

6

7

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:结束某个进程

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 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);

}

需要的权限

1

2

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

模拟HOME按键

1

2

3

4

Intent i=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 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:发送文件

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 path) {

File mZipFile=new File(path);

Intent intent = new Intent(Intent.ACTION_SEND);

//  intent.setClassName("com.android.bluetooth", "com.broadcom.bt.app.opp.OppLauncherActivity");

// intent.setClassName("com.android.bluetooth", "com.android.bluetooth.opp.BluetoothOppLauncherActivity");

intent.putExtra("subject", mZipFile

.getName()); //

intent.putExtra("body", "content by chopsticks"); // 正文

intent.putExtra(Intent.EXTRA_STREAM,

Uri.fromFile(mZipFile)); // 添加附件,附件为file对象

if (mZipFile.getName().endsWith(".gz")) {

intent

.setType("application/x-gzip"); // 如果是gz使用gzip的mime

} else if (mZipFile.getName().endsWith(

".txt")) {

intent.setType("text/plain"); // 纯文本则用text/plain的mime

} else if (mZipFile.getName().endsWith(

".zip")) {

intent.setType("application/zip"); // 纯文本则用text/plain的mime

} else {

intent

.setType("application/octet-stream"); // 其他的均使用流当做二进制数据来发送

}

// startActivity(intent);

startActivity(

Intent.createChooser(intent,

"选择蓝牙客户端"));

}

Android常用代码集合的更多相关文章

  1. Android 常用代码大集合 [转]

    [Android]调用字符串资源的几种方法   字符串资源的定义 文件路径:res/values/strings.xml 字符串资源定义示例: <?xml version="1.0&q ...

  2. phpcms v9模板制作常用代码集合(转)

    phpcms v9模板制作常用代码集合(个人收藏) 1.截取调用标题长度 {str_cut($r[title],36,'')} 2.格式化时间 调用格式化时间 2011-05-06 11:22:33 ...

  3. phpcms v9模板制作常用代码集合

    phpcms v9模板制作常用代码集合(个人收藏) 1.截取调用标题长度 {str_cut($r[title],36,'')} 2.格式化时间 调用格式化时间 2011-05-06 11:22:33 ...

  4. SAP屏幕字段常用代码集合

    SAP屏幕字段常用代码集合 ().Screen 设计 TABLES: SSCRFIELDS. PARAMETERS: P_EBLEN LIKE VBRK-EBLEN DEFAULT ' '. PARA ...

  5. ExtJS常用代码集合

    ExtJS常用代码集合,包括弹出提示框,登陆框,树状结构等等.​1. [代码]弹出提示框     <html>    <head>        <title>Ge ...

  6. C#常用代码集合(1)

    引用自james li的博客,地址:http://www.cnblogs.com/JamesLi2015/p/3147986.html   1 读取操作系统和CLR的版本   OperatingSys ...

  7. Android常用代码

    1.图片旋转 Bitmap bitmapOrg = BitmapFactory.decodeResource(this.getContext().getResources(), R.drawable. ...

  8. Android 常用代码片小结

    1. dp px 相互转换---------------public class DensityUtil { /** * 根据手机的分辨率从 dip 的单位 转成为 px(像素) */ public ...

  9. Android 常用代码

    1.单元测试 然而可以直接建立单元测试 <uses-library android:name="android.test.runner"/> 放在application ...

随机推荐

  1. Delphi 操作word 表格

    var wordApp, WordDoc, WrdSelection, wrdtable: variant; strAdd: string; wdPar,wdRange:OleVariant; iCo ...

  2. 仿酷狗音乐播放器开发日志二十四 选项设置窗体的实现(附328行xml布局源码)

    转载请说明原出处,谢谢~~ 花了两天时间把仿酷狗的选项设置窗体做出来了,当然了只是做了外观.现在开学了,写代码的时间减少,所以整个仿酷狗的工程开发速度减慢了.今天把仿酷狗的选项设置窗体的布局代码分享出 ...

  3. 仿酷狗音乐播放器开发日志十一——CTreeNodeUI的bug修复

    由于做播放列表控件,我的CMusicLength控件继承了CTreeVieWUI控件,在向分组控件中添加播放项目时,发现代码无法正常工作,调用CTreeNodeUI控件的Add方法后无反应,导致我的播 ...

  4. 【C++对象模型】构造函数语意学之二 拷贝构造函数

    关于默认拷贝构造函数,有一点和默认构造函数类似,就是编译器只有在[需要的时候]才去合成默认的拷贝构造函数. 在什么时候才是[需要的时候]呢? 也就是类不展现[bitwise copy semantic ...

  5. java易混淆概念之类变量、成员变量、局部变量

      类变量.成员变量.局部变量 类变量(也叫静态变量)是类中独立于方法之外的变量,用static 修饰.(static表示“全局的”.“静态的”,用来修饰成员变量和成员方法,或静态代码块(静态代码块独 ...

  6. Matlab中imshow()函数的使用

    imread() 返回的图像类型是uint8类型, 这时用imshow显示图像的时候, imshow会认为输入矩阵的范围在0-255, 如果imshow的参数为double类型的,那么imshow认为 ...

  7. Tkinter教程之Pack篇

    本文转载自:http://blog.csdn.net/jcodeer/article/details/1813077 '''Tkinter教程之Pack篇'''#Pack为一布局管理器,可将它视为一个 ...

  8. windows7+eclipse-jee-luna+hadoop2.6运行环境及eclipse plugin插件编译

    一.hadoop集群环境配置  参见:<Hadoop2.6集群环境搭建(HDFS HA+YARN)原来4G内存也能任性一次.> Win7环境: 登录用户名:hadoop , 与Hadoop ...

  9. .NET Reactor 命令行使用

    安装.NET Reactor工具软件.例如你的安装目录为:D:\Program Files\Eziriz\.NET Reactor 按如下步骤设置系统环境变量path. 将path变量的值中加入.NE ...

  10. JSP学习初体验

    JSP简介: 1)JSP--Java Server Pages 2)拥有servlet的特性与优点(本身就是一个servlet) 3)直接在HEML中内嵌JSP代码 4)JSP程序由JSP Engin ...