第一行代码Android学习:第十二部分主要涉及到全局获取context的技巧、使用Intent传递对象、定制自己的日志工具
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dyhdm_12_00test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:name="com.example.dyhdm_12_00test.MyApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.dyhdm_12_00test.MainActivity"
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="com.example.dyhdm_12_00test.Main2Activity"
android:label="@string/title_activity_main2" >
activity>
application>
manifest>
<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"
tools:context=".MainActivity" >
<Button
android:id="@+id/bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
RelativeLayout>
<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"
tools:context=".Main2Activity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world2" />
RelativeLayout>
/*
* @Title: MyApplication.java
* @Description: TODO
* @author: 张志安
* @date: 2016-8-30 上午10:12:46
*
*/
package com.example.dyhdm_12_00test;
import android.app.Application;
import android.content.Context;
/**
* TODO 定制一个自己的Application,为了个在项目的任何地方都能获取到Context.
* 注意要修改AndroidManifest.xml
* 获取Context:MyApplication.getContext()
* @author 张志安
* @date: 2016-8-30 上午10:12:46
*/
public class MyApplication extends Application {
public static Context context;
@Override
public void onCreate() {
context = getApplicationContext();
}
public static Context getContext(){
return context;
}
}
/*
* @Title: Person.java
* @Description: TODO
* @author: 张志安
* @date: 2016-8-30 上午10:25:06
*
*/
package com.example.dyhdm_12_00test;
import java.io.Serializable;
/**
* TODO 实现序列化接口的Person类 所有的Person对象都可以序列化
*
* @author 张志安
* @date: 2016-8-30 上午10:25:06
*/
public class Person implements Serializable {
private String name;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
/*
* @Title: LogUtil.java
* @Description: TODO
* @author: 张志安
* @date: 2016-8-30 上午10:34:55
*
*/
package com.example.dyhdm_12_00test;
import android.util.Log;
/**
* TODO 定制自己的日志工具
* 打印一行DEBUG级别的日志可以写成LogUtil.d("zza","123")
* 只需要修改LEVEL的值就可以自由控制打印行为
*
* @author 张志安
* @date: 2016-8-30 上午10:34:55
*/
public class LogUtil {
public static final int VERBOSE = 1;
public static final int DEBUG = 2;
public static final int INFO = 3;
public static final int WARN = 4;
public static final int ERROR = 5;
public static final int NOTHING = 6;
public static final int LEVEL = VERBOSE;
public static void v(String tag, String msg) {
if (LEVEL <= VERBOSE) {
Log.v(tag, msg);
}
}
public static void d(String tag, String msg) {
if (LEVEL <= DEBUG) {
Log.d(tag, msg);
}
}
public static void i(String tag, String msg) {
if (LEVEL <= INFO) {
Log.i(tag, msg);
}
}
public static void w(String tag, String msg) {
if (LEVEL <= WARN) {
Log.w(tag, msg);
}
}
public static void e(String tag, String msg) {
if (LEVEL <= ERROR) {
Log.e(tag, msg);
}
}
}
package com.example.dyhdm_12_00test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button bt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 测试MyApplication.getContext()
Toast.makeText(MyApplication.getContext(), "Test", Toast.LENGTH_SHORT)
.show();
// 测试使用Intent传递对象
bt = (Button) findViewById(R.id.bt);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Person p = new Person();
p.setAge("21");
p.setName("zza");
Intent intent = new Intent(MyApplication.getContext(),
Main2Activity.class);
intent.putExtra("data", p);
startActivity(intent);
}
});
// 测试定制的日志工具
LogUtil.i("zza", "123");
}
}
package com.example.dyhdm_12_00test;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Toast;
public class Main2Activity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 获取通过intent传递的对象
Person p = (Person) getIntent().getSerializableExtra("data");
setContentView(R.layout.activity_main2);
Toast.makeText(MyApplication.getContext(),
"Name:" + p.getName() + " Age:" + p.getAge(),
Toast.LENGTH_SHORT).show();
}
}
代码下载地址