chenkan / Ni

尘泥大杂烩
1 stars 1 forks source link

安卓学习全纪录 #9

Open chenkan opened 11 years ago

chenkan commented 11 years ago

学习学习:-)

chenkan commented 11 years ago

First Blood

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="24dp"
        android:ems="10" >

        <requestFocus />
    </EditText>

</RelativeLayout>
package com.example.demo;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;

public class MainActivity extends Activity {

    private static final String TAG = "ActivityDemo";
    private EditText mEditText;
    //定义一个String 类型用来存取我们EditText输入的值
    private String mString;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mEditText = (EditText)findViewById(R.id.editText1);
        Log.e(TAG, "start onCreate~~~");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.e(TAG, "start onStart~~~");
    }

    //当按HOME键时,然后再次启动应用时,我们要恢复先前状态
    @Override
    protected void onRestart() {
        super.onRestart();
        mEditText.setText(mString);
        Log.e(TAG, "start onRestart~~~");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e(TAG, "start onResume~~~");
    }

    //当我们按HOME键时,我在onPause方法里,将输入的值赋给mString
    @Override
    protected void onPause() {
        super.onPause();
        mString = mEditText.getText().toString();
        Log.e(TAG, "start onPause~~~");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.e(TAG, "start onStop~~~");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "start onDestroy~~~");
    }

}
chenkan commented 11 years ago

知识点

在网页的世界里,想要在两个页面间的转换,只要利用超链接就可以实现
但是在手机的世界里,要如何实现手机页面的转换呢? 最简单的方法就是改变Activity 的Layout
package com.example.demo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

/**
 * 在网页的世界里,想要在两个页面间的转换,只要利用超链接就可以实现
 * 但是在手机的世界里,要如何实现手机页面的转换呢? 最简单的方法就是改变Activity 的Layout
 */
public class MainActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 载入main.xml Layout
        goToLayout1();
    }

    // 将layout由main.xml切换成mylayout.xml
    public void goToLayout2() {
        // 将layout改成mylayout
        setContentView(R.layout.mylayout);
        Button bt2 = (Button) findViewById(R.id.bt2);
        bt2.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                goToLayout1();
            }
        });
    }

    // 将layout由mylayout.xml切换成main.xml
    public void goToLayout1() {
        setContentView(R.layout.main);
        Button bt1 = (Button) findViewById(R.id.bt1);
        bt1.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                goToLayout2();
            }
        });
    }

}
chenkan commented 11 years ago

知识点

package com.example.demo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button; 

public class IntentDemo extends Activity {

    private Button bt1;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        bt1 = (Button)findViewById(R.id.bt1);
        bt1.setOnClickListener(new Button.OnClickListener(){
            public void onClick(View v){
                //new 一个Intent对象,并指定要启动的Class
                Intent intent = new Intent();              
                intent.setClass(IntentDemo.this, IntentDemo1.class);          
                //调用一个新的Activity
                startActivity(intent);
                //关闭原本的Activity
                IntentDemo.this.finish(); 
            }
        });
    }
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.demo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.demo.IntentDemo"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="IntentDemo1"></activity> 
    </application>

</manifest>
chenkan commented 11 years ago

知识点

    <RadioGroup
        android:id="@+id/radiogroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="3px"
        android:orientation="vertical" >

        <RadioButton
            android:id="@+id/rb1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_x="60px"
            android:layout_y="65px"
            android:checked="true"
            android:text="man" />

        <RadioButton
            android:id="@+id/rb2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_x="160px"
            android:layout_y="65px"
            android:text="woman" />
    </RadioGroup>

创建Bundle塞入参数

package com.example.demo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

public class IntentDemo extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button bt1 = (Button) findViewById(R.id.bt1);
        bt1.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                // 取得name
                EditText et = (EditText) findViewById(R.id.ed1);
                String name = et.getText().toString();

                // 取得Gender
                String sex;
                RadioButton rb1 = (RadioButton) findViewById(R.id.rb1);
                if (rb1.isChecked()) {
                    sex = "man";
                } else {
                    sex = "woman";
                }

                // new一个Intent对象,用法上节已经讲过了
                Intent intent = new Intent();
                intent.setClass(IntentDemo.this, IntentDemo1.class);

                // new一个Bundle对象,并将要传递的数据导入,这里是重点Map<String,String>结构哦
                Bundle bundle = new Bundle();
                bundle.putString("name", name);
                bundle.putString("sex", sex);
                // 将Bundle对象assign给Intent
                intent.putExtras(bundle);

                // 调用Activity
                startActivity(intent);
            }
        });
    }
}

Bundle中读出参数

package com.example.demo;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class IntentDemo1 extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mylayout);

        // 取得Intent中的Bundle对象
        Bundle bundle = this.getIntent().getExtras();
        // 取得Bundle对象中的数据
        String name = bundle.getString("name");
        String sex = bundle.getString("sex");

        // 设置输出文字
        TextView mytv = (TextView) findViewById(R.id.mytv);
        mytv.setText("you name is:" + name + "\n you gender is:" + sex);
    }
}
chenkan commented 11 years ago

music.mp3导入/res/raw文件夹

play.png / pause.png / stop.png导入/res/drawable文件夹

使用静态资源(mp3文件)

xxx
private MediaPlayer mp3;
xxx
// 创建MediaPlayer对象,使用/res/raw文件夹下的music.mp3
mp3 = MediaPlayer.create(this, R.raw.music);

使用静态资源(png文件)

<ImageButton
    xxx
    android:src="@drawable/pause"
    xxx
  >
xxx
private ImageButton buttonPause;
xxx
buttonPause = (ImageButton) findViewById(R.id.bt_pause);
chenkan commented 11 years ago

浏览了Emmagee两个核心Java文件,做了一些笔记(笔记处均带有ChenNi关键字)

详见:

还须进一步学习的知识点: 【1】

// TODO ChenNi - 了解Activity下所有API

【2】

@Override
    protected void onStart() {
        Log.d(LOG_TAG, "onStart");
        receiver = new UpdateReceiver();                        // FIXME ChenNi - 问一下二爷作用
        IntentFilter filter = new IntentFilter();
        filter.addAction("com.netease.action.emmageeService");
        this.registerReceiver(receiver, filter);
        super.onStart();
    }

【3】

// FIXME ChenNi - onStart VS onResume

【4】

protected void onStop() {
        unregisterReceiver(receiver);   // ChenNi - 与onStart()对照看
        super.onStop();
    }

【5】

// FIXME ChenNi - Activity VS Service
// FIXME ChenNi - Service生命周期

【6】

registerReceiver(batteryBroadcast, new IntentFilter("android.intent.action.BATTERY_CHANGED"));  // TODO ChenNi - 问一下二爷
// TODO ChenNi - See "IntentFilter"

【7】

public class BatteryInfoBroadcastReceiver extends BroadcastReceiver {
// TODO ChenNi - WHAT IS BroadcastReceiver

【8】

/**
         * ChenNi - 【注意】创建一个浮窗
         */
        windowManager = (WindowManager) getApplicationContext().getSystemService("window");
        wmParams = ((MyApplication) getApplication()).getMywmParams();
        wmParams.type = 2002;
        wmParams.flags |= 8;
        wmParams.gravity = Gravity.LEFT | Gravity.TOP;
        wmParams.x = 0;
        wmParams.y = 0;
        wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        wmParams.format = 1;
        windowManager.addView(viFloatingWindow, wmParams);

【9】

Intent intent = new Intent();
                intent.putExtra("isServiceStop", true);
                intent.setAction("com.netease.action.emmageeService");
                sendBroadcast(intent);  // TODO ChenNi
                stopSelf();

通过阅读Emmagee源码,迅速探明了一些须要掌握的知识点,以利于后续学习

chenkan commented 11 years ago

首先你要知道Activity的四种状态: Active/Runing 一个新 Activity 启动入栈后,它在屏幕最前端,处于栈的最顶端,此时它处于可见并可和用户交互的激活状态。 Paused 当 Activity 被另一个透明或者 Dialog 样式的 Activity 覆盖时的状态。此时它依然与窗口管理器保持连接,系统继续维护其内部状态,所以它仍然可见,但它已经失去了焦点故不可与用户交互。 Stoped 当 Activity 被另外一个 Activity 覆盖、失去焦点并不可见时处于 Stoped 状态。 Killed Activity 被系统杀死回收或者没有被启动时处于 Killed 状态。

来源于百度百科:http://zhidao.baidu.com/question/362569833.html