【loading】Android常用代码集合
来源:互联网 发布:手机音乐编辑软件 编辑:程序博客网 时间:2024/05/22 15:18
这篇文章主要记录一些常用的一些代码段,方便以后查阅,不断更新中。
1:调用浏览器,载入某网址
Uri uri = Uri.parse(
"http://www.android-study.com"
);
Intent it =
new
Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
2:Broadcast接收系统广播的intent监控应用程序包的安装、删除
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
<?
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输出一个字符串
public
void
DisplayToast(String str) {
Toast.makeText(
this
,str,Toast.LENGTH_SHORT).show();
}
4:把一个字符串写进文件
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:把文件内容读出到一个字符串
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安装和卸载程序
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:结束某个进程
activityManager.restartPackage(packageName);
8:设置默认来电铃声
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);
}
需要的权限
<
uses-permission
android:name
=
"android.permission.WRITE_SETTINGS"
></
uses-permission
>
模拟HOME按键
Intent i=
new
Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
9:打开某一个联系人
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:发送文件
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,
"选择蓝牙客户端"
));
}
//安装apk文件
private void installAPK(File file) {
//卸载apk文件
//从SIM卡中获取联系人
private Cursor getContacts() {
Uri uri = Uri.parse("content://sim/adn");
String[] projection = new String[] { "name", "phone" };
String selection = null;
String[] selectionArgs = null;
String sortOrder = null;
return managedQuery(uri, projection, selection, selectionArgs,sortOrder);
}
- 【loading】Android常用代码集合
- Android 常用代码集合
- Android 常用代码集合
- Android 常用代码集合
- Android常用代码集合
- Android 常用代码集合
- Android 常用代码集合
- 转载:Android 常用代码集合
- javascript常用代码集合
- JavaScript常用代码集合
- 常用代码集合
- Android 转菊花控件:loading加载常用
- android 常用方法集合
- android 常用方法集合
- android 常用方法集合
- Android常用Dialog集合
- Android 常用框架集合
- Android 常用框架集合
- win8 Dota全屏设置
- 双向关联:多对多
- play framework中实现分页
- (Manual) Interactive Data Visualization for the Web
- 机器学习实战Logistic回归之马儿得疝气了,怎么办?
- 【loading】Android常用代码集合
- 提示框的显示与消失
- cocos2d-helloworld
- 字符串专项:UVa 11855
- 关于socket长连接的心跳包
- Android 监控程序安装和删除的实现
- java的double的大数值时 怎么才能不显示成科学计数法
- mysql group by 用法解析(详细)
- 开源喽,特别适合新手学习的一个系统。附有一个快速入门计划!《一句话发布系统》- One Word Delivery System