②在AndroidManifest.xml中修改相应Activity的theme
<activity android:name=".HomeActivity"
android:label="@string/app_name"
android:theme="@style/AlexNoAniTheme">
或者将 @style/noAnimation加入Activity原来的theme
源码修改过程中遇到不少或大或小的问题,接着昨天总结吧。
12.设置分割线
横线:
<View
android:layout_width=“fill_parent”
android:layout_height=“1px”
android:background="?android:attr/listDivider"
/>
竖线:
<View
android:layout_width=“1px”
android:layout_height=“fill_parent”
android:background="?android:attr/listDivider" />
13.调用系统发短信界面
Uri uri = Uri.parse(“smsto:” + callLog.getNumber()); // callLog.getNumber()为手机号码
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
//it.putExtra(“sms_body”, “这是填充短信内容”);
ctx.startActivity(it);
14.adb查看真机log
adb logcat *:E // Error级别,其他可类比
15.锁定Activity的屏幕方向
在Manifest文件中对应Activity属性中添加: //在application中添加是不是整个应用都有效?不是
android:screenOrientation=“nosensor”
16.Toast常犯错误
Toast.makeText(this, text, Toast.LENGTH_SHORT).show(); // 常犯错误:忘了调用 show 方法,调试的时候死活不见吐司,搞的晕头转向,生生没有看见ADT的高亮显示,结果是这么明显的错误
17. LinearLayout
也可以有onClick属性,也可以findById,哈哈。这个很好用啊。
18.ActionBar返回上一层
import android.app.ActionBar;
Activity onCreate()中:
ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
在
public boolean onOptionsItemSelected(MenuItem item) 中添加一个case:
case android.R.id.home:
finish();
break;
19.在Adapter中进行跳转Activity
不同于Activity,Adapter中的跳转用上Context就可以了。
/*
*Everything needs a context :(
*/
// RecordAdapter.javaprivate Context ctx;
对应跳转代码:
Intent intent = new Intent(ctx,
com.android.dialer.calllog.RecordDetail.class);
intent.putExtra(“mNum”, callLog.getNumber());
ctx.startActivity(intent);
//以下代码是Activity跳转动画,可忽略:
// add for animation START
int version = Integer.valueOf(android.os.Build.VERSION.SDK);
if (version > 5) {
((Activity)ctx).overridePendingTransition(
com.android.dialer.R.anim.push_left_in,
com.android.dialer.R.anim.push_left_out);
}
// add for animation END
20.overflow menu内容显示图标
其实,overflow中的Action按钮应不应该显示图标,是由MenuBuilder这个类的setOptionalIconsVisible变量来决定的,如果我们在overflow被展开的时候将这个变量赋值为true,那么里面的每一个Action按钮对应的图标就都会显示出来了。赋值的方法仍然是用反射了,代码如下:
@Override
public boolean onMenuOpened(int featureId, Menu menu) {
if (featureId == Window.FEATURE_ACTION_BAR && menu != null) {
if (menu.getClass().getSimpleName().equals(“MenuBuilder”)) {
try {
Method m = menu.getClass().getDeclaredMethod(“setOptionalIconsVisible”, Boolean.TYPE);
m.setAccessible(true);
m.invoke(menu, true);
} catch (Exception e) {
}
}
}
return super.onMenuOpened(featureId, menu);
}
21.View的setVisibiility()
android view setVisibility():
有三个参数:Parameters:visibility One of VISIBLE, INVISIBLE, or GONE,想对应的三个常量值:0、4、8
VISIBLE:0 意思是可见的
INVISIBILITY:4 意思是不可见的,但还占着原来的空间
GONE:8 意思是不可见的,不占用原来的布局空间
22.联系人接口
添加号码到联系人或新建联系人:
Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
intent.setType(“vnd.android.cursor.item/contact”);
intent.putExtra(Intents.Insert.PHONE, number);
startActivity(intent);
直接添加联系人:
Intent intent = new Intent(Contacts.Intents.Insert.ACTION);
intent.setType(Contacts.People.CONTENT_TYPE);
intent.putExtra(Contacts.Intents.Insert.NAME, “zhangsan”);
intent.putExtra(Contacts.Intents.Insert.PHONE, number);
intent.putExtra(Contacts.Intents.Insert.PHONE_TYPE,Contacts.PhonesColumns.TYPE_MOBILE);
intent.putExtra(Contacts.Intents.Insert.EMAIL, “”);
intent.putExtra(Contacts.Intents.Insert.COMPANY,“http://orgcent.com”);
startActivity(intent);
=====
根据number获取ContactId,然后编辑指定联系人:
String str = getContactId(this, number);
long contactId = Long.parseLong(str);
//Toast.makeText(this, “contactId:”+contactId, 2).show();
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setData(ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,contactId));
startActivity(intent);
// zj:根据号码获取ContactId
public static String getContactId(Context context, String number) {
Cursor c = null;
try {
c = context.getContentResolver().query(Phone.CONTENT_URI,
new String[] { Phone.CONTACT_ID, Phone.NUMBER }, null,
null, null);
if (c != null && c.moveToFirst()) {
while (!c.isAfterLast()) {
if (PhoneNumberUtils.compare(number, c.getString(1))) {
return c.getString(0);
}
c.moveToNext();
}
}
} catch (Exception e) {
// Log.e(TAG, “getContactId error:”, e);
} finally {
if (c != null) {
c.close();
}
}
return null;
}
今天就到这儿吧,明天继续。
晚上出去逛了一下,刚刚回来,虽然时间有点晚了,还是得继续啊,嘿嘿~
23.根据number获取联系人名字(若存在)
// zj add for CachedName -->RealName start
public String getContactNameByPhoneNumber(String number) {
if (TextUtils.isEmpty(number)) {
return null;
}
final ContentResolver resolver = ctx.getContentResolver();
Uri lookupUri = null;
String[] projection = new String[] { PhoneLookup._ID,
PhoneLookup.DISPLAY_NAME };
Cursor cursor = null;
try {
lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(number));
cursor = resolver.query(lookupUri, projection, null, null, null);