《Google!Android2手机应用程序设计入门》笔记(4)

说明:本周博客又要交白卷了,把空间里面的一篇日志转过来。

第四篇 中级篇(二)

 

chapter 17 加入新活动(Activity)

 

Activity的基本管理,个人总结如下。

1 新建。新建一个继承自Activity的类,实现(Override)onCreate等方法,一般还要添加用于布局等的xml文件,并用setContentView来关联这些xml文件。然后还要在AndroidManifest.xml文件中添加一个Activity节点,基本属性可以仿照默认的Ativity的属性来设置,这称为注册吧。这样就新建了一个Activity。

2 启动。使用Intent,示例代码:

Intent intent = new Intent();

intent.setClass(Bmi.this, Report.class);

startActivity(intent);

上面代码是要启动类名是Report的Activity。

3 Activity间数据传递,使用Bundle

发送方的示例代码,在上面启动代码的startActivity之前,加上这样的代码:

Bundle bundle = new Bundle();

bundle.putString(”KEY_HEIGHT",field_height.getText().toString());

...

intent.putExtras(bundle);

接收方的示例代码,一般在onCreate方法里面来接受前一个Acitity传来的数据:

Bundle  bunde = this.getIntent().getExtras();

double height = Double.parseDouble(bunde.getString("KEY_HEIGHT"))/100;

这样height的值就是前面field_height.getText().toString()的值。

在这里的Bundle就像是一个非常只能的字典。

 

chapter 18 传送数据到新意图(Intent)

 

前面一并整理了。

 

chapter 19 信息提醒

 

protected void showNotification(double  BMI){

    NotificationManager barManager = (NotificationManager)

        getSystemService(NOTIFICATION_SERVICE);

    Notification barMsg = new Notification(

        R.drawable.icon,

        "欧,你过重啰!",

        system.currentTimeMillis()

    );

    PendingIntent contentIntent = PendingIntent.getActivity(

        this,

        0,

        new Intent(this, Bmi.class),

        PendingIntent.FLAG_UPDATE_CURRENT);

    barMsg.setLatestEventInfo(

        Report.this,

        "你的BMI值过高",

        “通知监督人”,

        contentIntent

    );

    barManager.notify(0,barMsg);

}

 

chapter 20 记录与差错(Log)

 

1  Log.d(TAG, "find Views")

2  虚拟机上的查错设置: DevTools -> Developer Settings -> Show CPU Usage

 

chapter 21 活动的生命周期(LifeCycle)

 

这章计划单独研究一番。

 

chapter 22 存储信息(Preference)

 

1 存储偏好设置

@Override

 

public static final String PREF = "BMI_PREF";

public static final String PREF_HEIGHT = "BMI_HEIGHT";

 

protected void onPause() {

    super.onPause()

    SharedPreference settings = getSharedPreferences(PREF,0);

    settings.edit()

        .putString(PREF_HEIGHT, field_height.getText().toString())

        .commit();

}

在Activity的Pause之前存储部分数据是比较合适的。

2 取得偏好设置

private void restorePrefs(){

    SharedPreference settings = getSharedPreference(PREF,0);

    String pref_height = settings.getString(PREF_HEIGHT,"");

    if(! "".equals(pref_height)){

        field_height.setText(pref_height);

        field_weight.requestFocus();

    }

}

 

chapter 23 加入单元测试

 

和21章一样,要专门研究一番,书上给出了单元测试的一种方法。

 

chapter 24 开发不息

 

这一章是个过渡章节吧,主要是引出下一篇。

 
posted @ 2012-04-21 16:11  tt-0411  阅读(297)  评论(0编辑  收藏  举报