Android api兼容性问题,记录给自己看的赖的去找

跳转到通信录界面问题:

try {
                                    Intent it=null;
                                    if (Build.VERSION.SDK_INT>22){
                                        it = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
                                    }else{
                                        it = new Intent(Intent.ACTION_PICK, Contacts.People.CONTENT_URI);
                                    }
                                    startActivityForResult(it, 1);
                                }catch (Exception e){
                                    e.printStackTrace();
                                }

系统8.0限制后台问题

第一种如果前台需要显示通知的话:这里先判断版本是否是大于26,大于使用NotificationChannel创建通,否则使用Notification.Builder创建通知,如果是使用了IntentSercive服务使用JobIntentSercive替换使用。

private void createNotify(){
        String ns = Context.NOTIFICATION_SERVICE;
        mManager = (NotificationManager)getSystemService(ns);
        if (Build.VERSION.SDK_INT>Build.VERSION_CODES.O){
            NotificationChannel channel=new NotificationChannel(CHANNEL_ID,"nitify",NotificationManager.IMPORTANCE_HIGH);
            channel.enableLights(true);
            channel.setLightColor(Color.RED);
            channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
            assert mManager!=null;
            mManager.createNotificationChannel(channel);
            builder=new Notification.Builder(mContext,CHANNEL_ID);
            notification=builder.setSmallIcon(R.mipmap.ic_launcher)
                    .setOnlyAlertOnce(true)//只响一次
                    .setTicker("开始")
                    .setWhen(System.currentTimeMillis())
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))//
                    .setContentTitle("通知")
                    .setContentIntent(pendingIntent)
                    .build();
        }else{
            notification=new Notification.Builder(mContext)
                    .setOnlyAlertOnce(true)//只响一次
                    .setTicker("开始")
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setWhen(System.currentTimeMillis())
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))//
                    .setContentTitle("通知")
                    .setContentIntent(pendingIntent)
                    .build();
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        startForeground(1,notification);
        return super.onStartCommand(intent, flags, startId);
    }

第二种前台不需要显示通知使用JobIntentSercive

public class AddressService extends JobIntentService {
    private static int _sericeId=100011;
    public static void start(Context context){
        Intent intent=new Intent(context,AddressService.class);
        intent.putExtra("data","1111");
        enqueueWork(context,AddressService.class,_sericeId,intent);//启动服务
    }
    @Override
    protected void onHandleWork(@NonNull Intent intent) {
        String data = intent.getStringExtra("data");
    }
}

上只要在MainActivity使用AddressService.start(this)调用就行,这里需要注意的需要在AndroidManifest.xml中添加权限


    

在添加服务的地方加入android:permission="android.permission.BIND_JOB_SERVICE"代码否则运行报错例:

 

你可能感兴趣的:(技术)