在Android项目中,遇到过的坑

来源:互联网 发布:信鸽推送demo php 编辑:程序博客网 时间:2024/04/27 15:20

更新时间 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.0int color = context.getResources().getColor(R.color.white);// a large iconBitmap 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 magicRemoteInput 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 {    }
更改后,会调用

@Overridepublic 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