您的位置:首页 > 移动开发 > Android开发

BaseActivity在Android开发中的应用

2013-12-21 13:39 337 查看
在android开发中,很多时候是编写Activity,这个是直接与用户打交道的界面。经常要用到,比如隐藏标题栏,提示信息,跳转到其他activity,添加应用统计等,很多工作都是重复性的,可以把这写经常要在activity用的功能,抽象出来,编写一个BaseActivity,让其他的Activity继承,这样就可以直接调用了。代码结构也要好看得多。

以下是一个BaseActivity的框架,供参考。你也可以根据你的需要在里面添加你要用到的功能。

直接上代码。

public class BaseActivity extends Activity {
//内存调试开关,应用正式发布时应关闭
protected boolean showDebugMsg = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub

super.onCreate(savedInstanceState);
//打印当前使用内存
debugMemory("onCreate");

}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
/*
* 调用百度移动统计api
*/
StatService.onPause(this);

debugMemory("onPause");
}

@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
/*
* 调用百度移动统计api
*/
StatService.onResume(this);
debugMemory("onResume");
}

@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
debugMemory("onStart");
}

@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
debugMemory("onStop");
}

/*
* hideTitleBar() 隐藏标题栏
*
* 参数:无
*
* 返回值:无
*/
public void hideTitleBar() {

requestWindowFeature(Window.FEATURE_NO_TITLE);

}

/*
* setFullScreen() 设置全屏
*
* 参数:无
*
* 返回值:无
*/

public void setFullScreen() {
//去除标题栏
requestWindowFeature(Window.FEATURE_NO_TITLE);

//设置全屏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

}

/*
* 界面切换方法
*/

public void forward (Class<?> classObj) {

Intent intent = new Intent();

intent.setClass(this, classObj);

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

this.startActivity(intent);

//this.finish();
}

/*
* 界面切换方法(带数据)
*/
public void forward (Class<?> classObj, Bundle params) {

Intent intent = new Intent();

intent.setClass(this, classObj);

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

intent.putExtras(intent);

//this.finish();
}

/*
* killApp() 关闭应用
*
* 参数:无
*
* 返回值:无
*/

public void killApp() {
Process.killProcess(Process.myPid());
}

/*
* call_up() 拨打电话
*
* 参数:电话号码
*
* 返回值:无
*/

public void call_up(String number) {

Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:" + number));

startActivity(intent);
}

/*
* toast()提示信息
*/
public void toast (String msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}

/*
* exitDialog() 退出系统对话框
*/
public void exitDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setMessage(R.string.ExitMessage)
.setCancelable(false)
.setPositiveButton(R.string.Yes,new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
killApp(); //退出应用
}
})
.setNegativeButton(R.string.Cancel,new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel(); //关闭对话框
}
});

AlertDialog alert = builder.create();

alert.show();
}

// public void loadingProgressDialog() {
// ProgressDialog dialog = ProgressDialog.show(this, "", "正在加载数据,请稍等...",true);
// }

public void debugMemory(String tag) {
if (this.showDebugMsg) {
Log.w(this.getClass().getSimpleName(),tag + ":" + AppUtil.getUsedMemory());
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  BaseActivity