在Android项目中,遇到过的坑

更新时间 2016-04-05

0,在 Application 中调用 Activity 中 a() 方法,而 a() 方法中用到了 Context 对象,此时崩溃。

解决办法:将 Context 形参赋值为 Application 的 Context 对象。


1,注意隐式Intent的运行检查

隐式Intent找不到合适的调用组件的情况,系统会抛出 ActivityNotFoundException的异常,解决方案:

Intent intent = new Intent(Intent.ACTION_XXX);
ComponentName name = intent.resolveActivity(context.getPackageManager());
if (name != null) {
    String className = name.getClassName();
}

2,使用 NotificationCompat 处理消息通知

// Add your color 5.0
int color = context.getResources().getColor(R.color.white);
// a large icon
Bitmap profileIcon = XXX;
builder.setLargeIcon(profileIcon);
builder.setStyle(new NotificationCompat.BigTextStyle(test));
PendingIntent pendingIntent = XXX;
builder.addAction(new NotificationCompat().Action(R.mipmap.ic_launcher, "Test", intent));

// 穿戴设备
NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender();
// do the magic
RemoteInput remoteInput = new RemoteInput.Builder("key").setLabel("replay").build();
wearableExtender.addAction(new NotificationCompat.Action.Builder(R.mipmap.ic_launcher, "replay", replayIntent)
.addRemoteInput(remoteInput).build());
builder.extend(wearableExtender);

3,Android 6.0 运行时权限

if (context.checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
    // already available
} else {
    
}
更改后,会调用

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == REQUEST_CAMERA) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            //
        } else {
            //
        }
    } else {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

4,使用 MediaSessionCompat

<action android name="android.intent.action.MEDIA_BUTTON"/>


5,使用Toolbar代替Actionbar

需要继承主题Theme.AppCompat.NoActionBar

<android.support.v7.widget.Toolbar/>

setSupportActionBar(toolbar);


6,使用AppBarLayout

7,使用searchView

8,DrawerLayout, NavigationView

v4包

9,Tabs and ViewPager



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值