




已阅读5页,还剩4页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
这篇文章主要记录一些我常用的一些代码段,方便以后查阅,不断更新中1 调用浏览器 载入某网址01.Uri uri = Uri.parse(); 02.Intent it = new Intent(Intent.ACTION_VIEW, uri); 03.startActivity(it); Uri uri = Uri.parse(); Intent it = new Intent(Intent.ACTION_VIEW, uri); startActivity(it); 2 Broadcast接收系统广播的intent 监控应用程序包的安装 删除01.public class getBroadcast extends BroadcastReceiver 02. Override 03. public void onReceive(Context context, Intent intent) 04. 05. if(Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction() 06. Toast.makeText(context, 有应用被添加, Toast.LENGTH_LONG).show(); 07. 08. else if(Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction() 09. 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.xml01. 02. 06. 07. 09. 10. 11. 12. 13. 14. 15. 16. 17. !- - 18. 19. 20. !- - 21. !- - 22. 23. 24. 25. 26. 27. 28. 29. !- - !- - !- - 3 使用Toast输出一个字符串01.public void DisplayToast(String str) 02. 03. Toast.makeText(this,str,Toast.LENGTH_SHORT).show(); 04. public void DisplayToast(String str) Toast.makeText(this,str,Toast.LENGTH_SHORT).show(); 4 把一个字符串写进文件01.public void writefile(String str,String path ) 02. 03. File file; 04. FileOutputStream out; 05. try 06. /创建文件 07. file = new File(path); 08. file.createNewFile(); 09. 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 plaincopy to clipboardprint?01.public String getinfo(String path) 02. 03. File file; 04. String str=; 05. FileInputStream in; 06. try 07. /打开文件file的InputStream 08. file = new File(path); 09. in = new FileInputStream(file); 10. /将文件内容全部读入到byte数组 11. int length = (int)file.length(); 12. byte temp = new bytelength; 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 bytelength; 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 plaincopy to clipboardprint?01.Intent intent = new Intent(Intent.ACTION_VIEW); 02. intent.setDataAndType(Uri.fromFile(new File(/sdcard/WorldCupTimer.apk), application/vnd.android.package-archive); 03. startActivity(intent); /安装 程序 04. 05. Uri packageURI = Uri.parse(package:zy.dnh); 06. Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI); 07. 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 plaincopy to clipboardprint?01.activityManager.restartPackage(packageName); activityManager.restartPackage(packageName); 8 设置默认来电铃声view plaincopy to clipboardprint?01.public void setMyRingtone() 02. 03. File k = new File(/sdcard/Shall We Talk.mp3); / 设置歌曲路径 04. ContentValues values = new ContentValues(); 05. values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath(); 06. values.put(MediaStore.MediaColumns.TITLE, Shall We Talk); 07. values.put(MediaStore.MediaColumns.SIZE, 8474325); 08. values.put(MediaStore.MediaColumns.MIME_TYPE, audio/mp3); 09. 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, Ring
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 英语笔译专业毕业论文
- 临床毕业论文
- 2024年公务员考试题库及一套参考答案
- 物流系毕业论文
- 行管本科毕业论文
- 风电项目风险评估与管理
- 老旧小区无障碍设施改造方案
- 2025医疗器械类考前冲刺练习试题及答案
- 临床医学概论题库及参考答案
- (2025)全国“安全生产月”知识培训测试试题和参考答案
- 生态农业开发授权委托书样本
- 糖尿病入院宣教护理
- 招聘与录用(第3版)课件全套 王丽娟 第1-8章 概述、招聘前的理论准备工作 -录用与招聘评估
- 黄色中国风家乡介绍山西
- 扬州树人学校2024-2025七年级上学期9月月考数学试卷及答案
- 报案材料范文模板
- 电商合伙经营合同
- 水利水电工程单元工程施工质量验收评定表及填表说明
- HG+20231-2014化学工业建设项目试车规范
- 《百变扭扭棒》大班艺术课件
- FZT 73013-2017 针织泳装行业标准
评论
0/150
提交评论