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的框架,供参考。你也可以根据你的需要在里面添加你要用到的功能。
直接上代码。
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());
}
}
}
相关文章推荐
- 【Android 应用开发】Android屏幕适配解析 - 详解像素,设备独立像素,归一化密度,精确密度及各种资源对应的尺寸密度分辨率适配问题
- windows下建立Android应用开发环境
- 谷歌Android手机应用开发环境的搭建 推荐
- Android嵌入式开发 开机启动系统应用开发
- Android应用开发环境搭建
- Android开发Intent应用概述
- Android应用开发提高系列(3)——《Effective Java 中文版》读书笔记
- 【Android 应用开发】BluetoothClass详解
- Android应用开发相关版本下载资源
- Android应用开发:搞定带标签滑动界面
- Titanium Mobile 编写一次就可以完成Android、BlackBerry 和 iPhone 的移动应用开发工具(黑莓开发是收费的)
- android应用开发学习纪实(6)
- Xamarin Android教程如何使用Xamarin开发Android应用
- IOS 与ANDROID框架及应用开发模式对比一
- android 平台上的手机应用开发 how to user HttpUrlConnection POST Much Fields to Web Server ?
- Android Andbase应用开发框架
- Android开发社交软件类应用时的若干问题
- Android将允许纯C/C++开发应用
- Android应用开发之(通过ClipboardManager, ClipData进行复制粘贴)
- Android平板上开发应用的一点心得——精确适配不同的dpi和屏幕尺寸