安卓.常用api

获取AndroidManifest.xml里面的版本号和名字

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mobilesafe1"
android:versionCode="3"
android:versionName="3" >

 

android.content.pm.PackageManager manager = getPackageManager();
android.content.pm.PackageInfo info = manager.getPackageInfo(getPackageName(), 0);
return info.versionName;

 

//取消标题栏
requestWindowFeature(Window.FEATURE_NO_TITLE);

// 完成窗体的全屏显示 // 取消掉状态栏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

 

 

 

动画效果展示:

ll_splash_main = (LinearLayout) this.findViewById(R.id.splash_main);
android.view.animation.AlphaAnimation aa = new android.view.animation.AlphaAnimation(0.0f, 1.0f);,, 完全透明到完全不透明显示
aa.setDuration(2000);,设置为2秒钟
ll_splash_main.startAnimation(aa);

 

 

 

获取value.xml里面配置的字符串

String path = context.getResources().getString(R.string.test);  后面个是数字

 

 

 

 

系统对话框:

 

android.app.AlertDialog.Builder buider = new android.app.AlertDialog.Builder(this);  新建一个对话框
buider.setIcon(R.drawable.icon5);  设置图标
buider.setMessage(uinfo.getDecription()+"\r\n"+uinfo.getApkUrl());  //设置提示内容
buider.setCancelable(false); // 让用户不能取消对话框


buider.setPositiveButton("确定", new android.content.DialogInterface.OnClickListener() {//给确定按钮设置点击事件
public void onClick(android.content.DialogInterface dialog, int which) {
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
//这里就开始下载
DownLoadFileThreadTask task = new DownLoadFileThreadTask(uinfo.getApkUrl(), "/sdcard/new.apk");
pd.show();
new Thread(task).start();
}else{
Toast.makeText(getApplicationContext(), "sd卡不可用", 1).show();
//不可用
loadMainUI();
}
}
});
buider.setNegativeButton("取消", new android.content.DialogInterface.OnClickListener() {//给取消按钮设置点击事件
public void onClick(android.content.DialogInterface dialog, int which) {
Log.i(TAG, "用户取消进入程序主界面");
//loadMainUI();
}
});

buider.create().show();//显示

}

 

 

 

加载另一个activity


/**
* 加载主页面
*/
private void loadMainUI() {
android.content.Intent intent = new android.content.Intent(this, MainActivity.class);
startActivity(intent);
finish(); // 把当前activity从任务栈里面移除
}

 

 

进度条的使用:

private ProgressDialog pd ;

pd = new ProgressDialog(this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setMessage("正在下载...");

pd.setMax(total);

pd.show();

。。开始操作

pd.dismiss();

 

 

 

 

安装apk:

/**
* 安装apk
* @param file
*/
private void install(java.io.File file){
android.content.Intent intent = new android.content.Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(android.net.Uri.fromFile(file), "application/vnd.android.package-archive");
finish();
startActivity(intent);
}

 

 

Handler和线程的配合使用

private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
super.handleMessage(msg);
Bundle data = msg.getData();
boolean val = data.getBoolean("value");
Toast.makeText(getApplicationContext(), val==true?"不需要更新":"需要更新", 1).show();
showUpdataDialog();
}

};

 

// 让当前的activity延时两秒钟 检查更新
new Thread(){
@Override
public void run() {
super.run();
try {
Message msg = new Message();
Bundle data = new Bundle();
data.putBoolean("value",IsNeedUpdate(version));
msg.setData(data);
handler.sendMessage(msg);
} catch (Exception e) {
e.printStackTrace();
}
}

}.start();
}

 

 

getapplication()和this上下文的区别,生命周期不同

getapplication是全部上下文,进程存在,他就存在

this表示,在任务栈它才会存在

打开数据库最好使用getapplication

 

 

 

 

取消标题:

 

<activity
android:label="@string/maintext"
android:theme="@android:style/Theme.NoTitleBar"//加上这个样式就可以了
android:name="com.example.mobilesafe.ui.MainActivity" >
</activity>

 

 

 

背景颜色的使用:

 

在drawable文件夹下面新建背景xml文件

 

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >长方形

<stroke
android:width="0.5dip"//边框大小
android:color="#ff505050" />//边框的颜色

<corners android:radius="2dip" >长方形的弧度
</corners>

<gradient android:startColor="#ff404040" 长方形的颜色,渐变
android:centerColor="#ff383838"
android:endColor="#ff404040"
/>
</shape>

 

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="150dip"
android:layout_height="150dip"
android:gravity="center_horizontal"
android:background="@drawable/item_background_selector"  //这个背景的使用
android:orientation="vertical" >

 

 

广播过滤器的使用:

 

<receiver android:name="com.example.mobilesafe.reciver.CallPhoneReceiver">  //这个是类
<intent-filter android:priority="1000">
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>

 

package com.example.mobilesafe.reciver;

import com.example.mobilesafe.ui.LostProtectedActivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class CallPhoneReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
String number = getResultData();
if("20122012".equals(number)){
Intent lostintent = new Intent(context,LostProtectedActivity.class);
lostintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//指定要激活的activity在自己的任务栈里面运行
context.startActivity(lostintent);
// 终止掉这个电话
// 不能通过 abortBroadcast();
setResultData(null);
}
}
}

 

需要加个权限:

<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>

posted @ 2014-05-04 13:33  宝贝,我永远都在  阅读(249)  评论(0)    收藏  举报