Skip to content

Commit 06ee123

Browse files
author
sundyzlh
committed
1 parent 0a13cd4 commit 06ee123

File tree

9 files changed

+235
-8
lines changed

9 files changed

+235
-8
lines changed

sundyandroid/Demos/SundyAndroidDemo/AndroidManifest.xml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,35 @@
33
package="sundy.android.demo"
44
android:versionCode="1"
55
android:versionName="1.0">
6+
7+
<uses-permission android:name="android.permission.READ_CONTACTS" />
8+
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
9+
<uses-permission android:name="android.permission.CAMERA" />
10+
<uses-permission android:name="android.permission.VIBRATE" />
11+
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
12+
<uses-permission android:name="android.permission.INTERNET" />
13+
614
<application android:icon="@drawable/icon" android:label="@string/app_name">
715
<activity android:name=".LauncherActivity"
816
android:label="@string/app_name">
917
<intent-filter>
1018
<action android:name="android.intent.action.MAIN" />
19+
<category android:name="android.intent.category.DEFAULT" />
1120
<category android:name="android.intent.category.LAUNCHER" />
1221
</intent-filter>
1322
</activity>
14-
23+
<activity android:name=".log.LogActivity" android:label="@string/subject_log">
24+
<intent-filter>
25+
<action android:name="android.intent.action.MAIN" />
26+
<category android:name="android.intent.category.SAMPLE_CODE" />
27+
</intent-filter>
28+
</activity>
29+
<activity android:name=".processthread.ProcessLifecycleActivity" android:label="@string/subject_process_lifecycle">
30+
<intent-filter>
31+
<action android:name="android.intent.action.MAIN" />
32+
<category android:name="android.intent.category.SAMPLE_CODE" />
33+
</intent-filter>
34+
</activity>
1535
</application>
1636
<uses-sdk android:minSdkVersion="3" />
1737

sundyandroid/Demos/SundyAndroidDemo/gen/sundy/android/demo/R.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,7 @@ public static final class layout {
1919
public static final class string {
2020
public static final int app_name=0x7f040001;
2121
public static final int hello=0x7f040000;
22+
public static final int subject_log=0x7f040002;
23+
public static final int subject_process_lifecycle=0x7f040003;
2224
}
2325
}

sundyandroid/Demos/SundyAndroidDemo/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22
<resources>
33
<string name="hello">Hello World, LauncherActivity!</string>
44
<string name="app_name">SundyAndroidDemo</string>
5+
<string name="subject_log">应用框架/其它/C11041401AAF_玩懂Log</string>
6+
<string name="subject_process_lifecycle">应用框架/进程和线程/C11041701AAF_进程生命周期</string>
57
</resources>

sundyandroid/Demos/SundyAndroidDemo/src/sundy/android/demo/LauncherActivity.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
import java.util.List;
99
import java.util.Map;
1010

11+
import sundy.android.demo.configration.CommonConstants;
12+
1113
import android.app.ListActivity;
1214
import android.content.Intent;
1315
import android.content.pm.PackageManager;
1416
import android.content.pm.ResolveInfo;
1517
import android.os.Bundle;
18+
import android.util.Log;
1619
import android.view.View;
1720
import android.widget.ListView;
1821
import android.widget.SimpleAdapter;
@@ -21,20 +24,23 @@ public class LauncherActivity extends ListActivity {
2124
/** Called when the activity is first created. */
2225
@Override
2326
public void onCreate(Bundle savedInstanceState) {
24-
super.onCreate(savedInstanceState);
27+
super.onCreate(savedInstanceState) ;
28+
2529
Intent intent = getIntent();
26-
String path = intent.getStringExtra("com.example.android.apis.Path");
30+
String path = intent.getStringExtra(CommonConstants.MAIN_LIST_VIEW_ITEMNAME);
2731

2832
if (path == null) {
2933
path = "";
3034
}
31-
35+
3236
setListAdapter(new SimpleAdapter(this, getData(path),
3337
android.R.layout.simple_list_item_1, new String[] { "title" },
3438
new int[] { android.R.id.text1 }));
3539
getListView().setTextFilterEnabled(true);
40+
3641
}
3742

43+
3844
protected List getData(String prefix) {
3945
List<Map> myData = new ArrayList<Map>();
4046

@@ -46,7 +52,6 @@ protected List getData(String prefix) {
4652

4753
if (null == list)
4854
return myData;
49-
5055
String[] prefixPath;
5156

5257
if (prefix.equals("")) {
@@ -107,7 +112,7 @@ protected Intent activityIntent(String pkg, String componentName) {
107112
protected Intent browseIntent(String path) {
108113
Intent result = new Intent();
109114
result.setClass(this, LauncherActivity.class);
110-
result.putExtra("com.example.android.apis.Path", path);
115+
result.putExtra(CommonConstants.MAIN_LIST_VIEW_ITEMNAME, path);
111116
return result;
112117
}
113118

@@ -121,7 +126,6 @@ protected void addItem(List<Map> data, String name, Intent intent) {
121126
@Override
122127
protected void onListItemClick(ListView l, View v, int position, long id) {
123128
Map map = (Map) l.getItemAtPosition(position);
124-
125129
Intent intent = (Intent) map.get("intent");
126130
startActivity(intent);
127131
}

sundyandroid/Demos/SundyAndroidDemo/src/sundy/android/demo/SundyAndroidDemoApplication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
*
2+
* Application of current instance management .
33
*/
44
package sundy.android.demo;
55

sundyandroid/Demos/SundyAndroidDemo/src/sundy/android/demo/configration/CommonConstants.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,10 @@
33
public class CommonConstants {
44
/**Application 打Log的时候的名称 -- "SundyAndroidDemoApplication" */
55
public static final String LOGCAT_APP_NAME = "SundyAndroidDemoApplication" ;
6+
/**主列表页面保存和恢复Intent的Key值*/
7+
public static final String MAIN_LIST_VIEW_ITEMNAME = "sundy.android.demo" ;
8+
/**Activity 打Log时候的名称 -- "SundyAndroidDemoLog" */
9+
public static final String LOGCAT_TAG_NAME = "SundyAndroidDemoLog" ;
10+
/** 应用程序的包名 */
11+
public static final String APP_PACKAGE_NAME = "sundy.android.demo" ;
612
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
*
3+
*/
4+
package sundy.android.demo.log;
5+
6+
import android.app.Activity;
7+
8+
/**
9+
* @author sundy
10+
*
11+
*/
12+
public class LogActivity extends Activity {
13+
14+
}
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/**
2+
* 熟悉进程生命周期的Demo类
3+
*/
4+
package sundy.android.demo.processthread;
5+
6+
import java.util.List;
7+
8+
import sundy.android.demo.configration.CommonConstants;
9+
import android.app.Activity;
10+
import android.app.ActivityManager;
11+
import android.app.ActivityManager.RunningAppProcessInfo;
12+
import android.app.AlertDialog;
13+
import android.app.Dialog;
14+
import android.content.DialogInterface;
15+
import android.os.Bundle;
16+
import android.os.Handler;
17+
import android.util.Log;
18+
import android.view.KeyEvent;
19+
import android.view.Menu;
20+
import android.view.MenuItem;
21+
22+
public class ProcessLifecycleActivity extends Activity {
23+
24+
/**当前进程信息实例 */
25+
RunningAppProcessInfo curRunningProcessInfo ;
26+
27+
/**
28+
* 初始化并且得到当前进程信息
29+
*/
30+
private void initCurProcess()
31+
{
32+
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE) ;
33+
List<RunningAppProcessInfo> processList = am.getRunningAppProcesses() ;
34+
for(int i=0 ; i<processList.size(); i++)
35+
{
36+
if(processList.get(i).processName.equals(CommonConstants.APP_PACKAGE_NAME))
37+
{
38+
curRunningProcessInfo = processList.get(i) ;
39+
return ;
40+
}
41+
}
42+
}
43+
44+
@Override
45+
public boolean onKeyUp(int keyCode, KeyEvent event) {
46+
// TODO Auto-generated method stub
47+
if(keyCode == KeyEvent.KEYCODE_HOME)
48+
{
49+
Log.i(CommonConstants.LOGCAT_TAG_NAME, "convertImportance(curRunningProcessInfo.importance)") ;
50+
}
51+
52+
return super.onKeyUp(keyCode, event);
53+
}
54+
55+
/**
56+
* 转换进程当前Level显示方式,讲int转为String
57+
* @param imp
58+
* @return
59+
*/
60+
protected String convertImportance(int imp)
61+
{
62+
String returnStr = null ;
63+
switch(imp)
64+
{
65+
case RunningAppProcessInfo.IMPORTANCE_FOREGROUND:
66+
returnStr = "IMPORTANCE_FOREGROUND" ;
67+
break ;
68+
case RunningAppProcessInfo.IMPORTANCE_VISIBLE:
69+
returnStr = "IMPORTANCE_VISIBLE" ;
70+
break ;
71+
case RunningAppProcessInfo.IMPORTANCE_SERVICE:
72+
returnStr = "IMPORTANCE_SERVICE" ;
73+
break ;
74+
case RunningAppProcessInfo.IMPORTANCE_BACKGROUND:
75+
returnStr = "IMPORTANCE_BACKGROUND" ;
76+
break ;
77+
case RunningAppProcessInfo.IMPORTANCE_EMPTY:
78+
returnStr = "IMPORTANCE_EMPTY" ;
79+
break ;
80+
81+
}
82+
return returnStr ;
83+
}
84+
85+
@Override
86+
protected void onCreate(Bundle savedInstanceState) {
87+
// TODO Auto-generated method stub
88+
super.onCreate(savedInstanceState);
89+
initCurProcess() ;
90+
Log.i(CommonConstants.LOGCAT_TAG_NAME, convertImportance(curRunningProcessInfo.importance)) ;
91+
}
92+
93+
@Override
94+
protected Dialog onCreateDialog(int id) {
95+
// TODO Auto-generated method stub
96+
if(id == 1)
97+
{
98+
AlertDialog.Builder builder = new AlertDialog.Builder(this);
99+
builder.setMessage("Are you sure you want to exit?")
100+
.setCancelable(false)
101+
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
102+
public void onClick(DialogInterface dialog, int id) {
103+
ProcessLifecycleActivity.this.finish();
104+
}
105+
})
106+
.setNegativeButton("No", new DialogInterface.OnClickListener() {
107+
public void onClick(DialogInterface dialog, int id) {
108+
dialog.cancel();
109+
}
110+
});
111+
AlertDialog alert = builder.create();
112+
return alert ;
113+
}
114+
115+
return super.onCreateDialog(id);
116+
}
117+
118+
@Override
119+
public boolean onCreateOptionsMenu(Menu menu) {
120+
// TODO Auto-generated method stub
121+
menu.add(0,0,1,"退出Activity") ;
122+
return super.onCreateOptionsMenu(menu);
123+
}
124+
125+
@Override
126+
public boolean onOptionsItemSelected(MenuItem item) {
127+
// TODO Auto-generated method stub
128+
129+
if(item.getItemId()==0)
130+
{
131+
this.showDialog(1) ;
132+
}
133+
134+
return super.onOptionsItemSelected(item);
135+
}
136+
137+
@Override
138+
protected void onPause() {
139+
// TODO Auto-generated method stub
140+
super.onPause();
141+
//Log.i(CommonConstants.LOGCAT_TAG_NAME, convertImportance(curRunningProcessInfo.importance)) ;
142+
}
143+
144+
@Override
145+
protected void onStop() {
146+
// TODO Auto-generated method stub
147+
super.onStop();
148+
Handler handler = new Handler() ;
149+
handler.postDelayed(new Runnable(){
150+
151+
public void run() {
152+
// TODO Auto-generated method stub
153+
Log.i(CommonConstants.LOGCAT_TAG_NAME, convertImportance(RunningAppProcessInfo.IMPORTANCE_BACKGROUND)) ;
154+
}
155+
156+
}, 3000) ;
157+
}
158+
159+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package sundy.android.demo.processthread;
2+
3+
import sundy.android.demo.configration.CommonConstants;
4+
import android.content.BroadcastReceiver;
5+
import android.content.Context;
6+
import android.content.Intent;
7+
import android.util.Log;
8+
9+
public class ProcessLifecycleBroadcastReceiver extends BroadcastReceiver {
10+
11+
@Override
12+
public void onReceive(Context arg0, Intent arg1) {
13+
// TODO Auto-generated method stub
14+
if (arg1.getAction().equals(Intent.CATEGORY_HOME))
15+
{
16+
Log.i(CommonConstants.LOGCAT_TAG_NAME, "go to HomeScreen") ;
17+
}
18+
}
19+
20+
}

0 commit comments

Comments
 (0)