LookupActivity是Android-SDK中的一个sample.
一、onNewIntent(Intent intent)方法
该方法是activity的一个保护类型的方法,利
用已有的Acivity去处理别的Intent时,你就可以利用onNewIntent来处理,通常被用在有搜索请求的activity,而其该activity有好几个intent-filter,该方法被调用的前提
a、该activity设置如下属性 android:launchMode="singleTop"。
b、该activity已经处在栈的顶端,通过其他的方法又重新启动该acitvity时被调用,如搜索,这时oncreate()方法不调用。
在这个例子中onNewIntent(Intent intent) 用来处理所有的Intent,在oncreate()方法中也认为调用了onNewIntent(getIntent());
public void onNewIntent(Intent intent) {
final String action = intent.getAction();
if (Intent.ACTION_SEARCH.equals(action)) {
// Start query for incoming search request
String query = intent.getStringExtra(SearchManager.QUERY);
startNavigating(query, true);
} else if (Intent.ACTION_VIEW.equals(action)) {
// Treat as internal link only if valid Uri and host matches
Uri data = intent.getData();
if (data != null && ExtendedWikiHelper.WIKI_LOOKUP_HOST
.equals(data.getHost())) {
String query = data.getPathSegments().get(0);
startNavigating(query, true);
}
} else {
// If not recognized, then start showing random word
startNavigating(null, true);
}
}
二、URI数据匹配
一个 Intent 可以通过 URI 携带外部数据给目标组件。在 <intent-filter >节点中,通过 <data/>节点匹配外部数据。
mimeType 属性指定携带外部数据的数据类型,scheme 指定协议,host、port、path 指定数据的位置、端口、和路径。如下:
<data android:mimeType="mimeType" android:scheme="scheme"
android:host="host" android:port="port" android:path="path"/>
如果在 Intent Filter 中指定了这些属性,那么只有所有的属性都匹配成功时 URI 数据匹配才会成功。
该例子中通过String.format("<a href=\"%s://%s/$1\">$1</a>", WIKI_AUTHORITY, WIKI_LOOKUP_HOST)));把链接格式化成<a href="wiktionary://lookup/ah">ah</a>形式的,
这样就可以和配置文件中的data相匹配
三、用<menu>标签创建菜单,在res/menu/目录下
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/lookup_search"
android:title="@string/lookup_search"
android:icon="@android:drawable/ic_menu_search" />
<item
android:id="@+id/lookup_random"
android:title="@string/lookup_random"
android:icon="@drawable/ic_menu_shuffle" />
<item
android:id="@+id/lookup_about"
android:title="@string/lookup_about"
android:icon="@android:drawable/ic_menu_help" />
</menu>
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.lookup, menu);
return true;
}
四、用布局文件填充AlerDialog
protected void showAbout() {
// Inflate the about message contents
View messageView = getLayoutInflater().inflate(R.layout.about, null, false);
// When linking text, force to always use default color. This works
// around a pressed color state bug.
TextView textView = (TextView) messageView.findViewById(R.id.about_credits);
int defaultColor = textView.getTextColors().getDefaultColor();
textView.setTextColor(defaultColor);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.app_icon);
builder.setTitle(R.string.app_name);
builder.setView(messageView);
builder.create();
builder.show();
}
五、用OnKeyDown事件控制返回键点击的频率,如果连击短于0.5秒,就返回桌面
private long mLastPress = -1;
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Handle back key as long we have a history stack
if (keyCode == KeyEvent.KEYCODE_BACK && !mHistory.empty()) {
// Compare against last pressed time, and if user hit multiple times
// in quick succession, we should consider bailing out early.
long currentPress = SystemClock.uptimeMillis();
if (currentPress - mLastPress < DateUtils.SECOND_IN_MILLIS / 2) {
return super.onKeyDown(keyCode, event);
}
mLastPress = currentPress;
// Pop last entry off stack and start loading
String lastEntry = mHistory.pop();
startNavigating(lastEntry, false);
return true;
}
// Otherwise fall through to parent
return super.onKeyDown(keyCode, event);
}
六、AsyncTask抽象类的使用
参考
http://www.youcanmobile.cn/bbs/viewthread.php?tid=54&extra=page%3D1
七、在strings.xml中定义数组
在java中代码中调用:context.getResources().getStringArray(R.array.month_names)
<string-array name="month_names">
<item>January</item>
<item>February</item>
<item>March</item>
<item>April</item>
<item>May</item>
<item>June</item>
<item>July</item>
<item>August</item>
<item>September</item>
<item>October</item>
<item>November</item>
<item>December</item>
</string-array>
八、点击桌面上的widget时,弹出activity
Intent defineIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(definePage));
PendingIntent pendingIntent = PendingIntent.getActivity(context,
0 /* no requestCode */, defineIntent, 0 /* no flags */);
RemoteViews.setOnClickPendingIntent(R.id.widget, pendingIntent);
//R.id.widget是布局文件的根元素的id