Android 常用代码集合

这篇博客汇总了Android开发中的常见功能实现,包括调用浏览器加载网址、接收系统广播(如应用安装、删除)、使用Toast显示消息、读写文件、安装卸载程序、结束进程、设置默认铃声、打开联系人以及发送文件等实用操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

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, 
                        "选择蓝牙客户端"));
	}


 

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值