这篇文章主要记录一些我常用的一些代码段,方便以后查阅,不断更新中
1 调用浏览器 载入某网址
2 Broadcast接收系统广播的intent 监控应用程序包的安装 删除
需要声明的权限如下AndroidManifest.xml
3 使用Toast输出一个字符串
4 把一个字符串写进文件
5 把文件内容读出到一个字符串
6 调用Android installer 安装和卸载程序
7 结束某个进程
8 设置默认来电铃声
需要的权限
模拟HOME按键
9 打开某一个联系人
10 发送文件
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,
"选择蓝牙客户端"));
}