http://blog.csdn.net/Zengyangtech/archive/2010/07/20/5748674.aspx
这篇文章主要记录一些我常用的一些代码段,方便以后查阅,不断更新中
1 调用浏览器 载入某网址
view plaincopy to clipboardprint?
Uri uri = Uri.parse("http://www.baidu.com");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
2 Broadcast接收系统广播的intent 监控应用程序包的安装 删除
view plaincopy to clipboardprint?
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
view plaincopy to clipboardprint?
<?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输出一个字符串
view plaincopy to clipboardprint?
public void DisplayToast(String str)
{
Toast.makeText(this,str,Toast.LENGTH_SHORT).show();
}
4 把一个字符串写进文件
view plaincopy to clipboardprint?
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?
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 调用Android installer 安装和卸载程序
view plaincopy to clipboardprint?
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?
activityManager.restartPackage(packageName);
8 设置默认来电铃声
view plaincopy to clipboardprint?
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);
;}
需要的权限
view plaincopy to clipboardprint?
<uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>
分享到:
相关推荐
电子书:Android常用的方法实例代码总结(PDF文档)
Android常用代码集合.pdf
教案Android常用代码集合.pdf
电子书:Android常用的方法实例代码总结(PDF文档).pdf
电子书:Android常用的方法实例代码总结(PDF文档)借鉴.pdf
Android_常用代码集合 常用代码 收集整理
Android常用代码!好东西!!!!!!
资深程序员多年代码实践总结:《和Android源代码一起工作》 | Android DevCamp 话题简介:本演讲将结合讲师多年的代码实践经验,主要介绍Android源代码以及相关内容,让应用开发者了解到如何利用Android源代码来...
Android高薪之路:Android程序员面试宝典
android部件代码集合,初学者了解基本开发使用
Android常用控件集合Demo大全,使用Android Studio编译
Android常用代码 经典不容错过
Android开发常用功能大集合。以及知识点的详解代码
android常用代码.pdf
Android Studio Plugins Spread Some :heart: This is a list of all awesome and useful android studio plugins. This repo will be updated regularly for new entries. Here is an article related to ...
Android stuido常用的插件集合都是最常用的插件,但是不好找的
Android开发常用整理(不断扩充中)包含各种工具类、线程池、日志、自定义的控件、程序崩溃捕捉处理、默认的Application配置、常用的Adapter等 注意: > 如果需要使用FragmentTabAdapter,则需要android-support-v4....
android:layout_width和android:layout_height用于设置控件的宽和高,match_parent表示匹配父容器,wrap_content则是与内容相适应。 android:text用于设置文字的内容 android:textColor用于设置文字的颜色 android:...