美文网首页
2018-12-24

2018-12-24

作者: 编程的猫 | 来源:发表于2018-12-27 21:34 被阅读0次

activity的启动模式:
生命周期:onCreate onStart onResume onPause onStop onDestroy onRestart
https://blog.csdn.net/zhangjg_blog/article/details/10923643
standard :标准启动模式,允许在占中存在相同的activity实例
singleTop:如果要启动的activity在栈顶,服用这个activity,否则创建一个新的activity,同standard
singleTask:如果所有activity的affinity属性都相同,要启动的目标activity不在task栈中,则添加一个activity到栈中,如果已经存在,则会移除该activity之上的其他activity,将自己置于栈顶。如果目标activity的affinity属性不同,则会创建一个新的task栈,并创建目标activity
singleInstance:会在单独的task栈中添加目标activity,并不允许其他activity存在栈中
service 非绑定启动 onCreate onStartCommand onDestroy
绑定启动 onCreate onBind onUNBind onDestroy

前台Service的写法

    public void onCreate() {
        super.onCreate();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
            startMyOwnForeground();
        else
            startForeground(1, new Notification());

//        Log.i("===TAG", "  myService onCreate  ");
//        //将Service服务编程前台服务
//        //构建点击通知后打开某个activity的Intent对象
//        Intent notificationIntent = new Intent(MyService.this, MainActivity.class);
//        PendingIntent pendingIntent = PendingIntent.getActivity(this,
//                0, notificationIntent, 0);
//        //新建Builder
//        Notification.Builder builder = new Notification.Builder(this);
//        //设置前台服务的标题
//        Notification notification = builder.setContentTitle("MyService标题")
//                .setContentText("前台服务的内容")
//                .setSmallIcon(R.mipmap.ic_launcher)
//                .setWhen(System.currentTimeMillis())
//                .setContentIntent(pendingIntent)
//                .build();
//        //将service变成前台服务,并在通知栏显示出来
//        startForeground(1,notification);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private void startMyOwnForeground(){
        String NOTIFICATION_CHANNEL_ID = "com.example.simpleapp";
        String channelName = "My Background Service";
        NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID,
                channelName, NotificationManager.IMPORTANCE_NONE);
        chan.setLightColor(Color.BLUE);
        chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        assert manager != null;
        manager.createNotificationChannel(chan);

        NotificationCompat.Builder notificationBuilder = new
                NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
        Notification notification = notificationBuilder.setOngoing(true)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("App is running in background" +
                        "App正在后台运行")
                .setPriority(NotificationManager.IMPORTANCE_MIN)
                .setCategory(Notification.CATEGORY_SERVICE)
                .build();
        startForeground(2, notification);
    }

远程Service

IPC跨进程通讯 AIDL Android接口定义语言

ContentProvider

BroadCastReciver

Fragment

生命周期:onAttach onCreate onCreateView onCreateActivity onStart onResume onPause onStop onDestroyView onDestroy onDettach

SQLite数据库

自定义一个类继承自SQLiteOpenHelper 重写onCreate upGrade和构造方法,执行db.execute方法创建一个数据库
DBUtil 。。。。增删改查的操作

Parcelable和Serializable数据序列化

实现都是实现相关接口,parcelable直接从内存中读取,serializable通过io流将数据读写到硬盘上,parcelable的内存优化高,Serializable的内存优化一般,parcelable数据不可持久化,Serializable数据可以持久化

网络请求

Volley

Volley.Requestqueue();
StringRequest sr= new StringRequest(StringRequest.Method.GET,url,new Response.Listener<String>(){
...
});

OkHttp3

Get请求

OkHttpClient client = new OkHttpClient();
Request request=new Request.Builder().url(url).builde();
Call call = client.newCall(request);
call.execute(
new CallBack(){
...
}
);

FormBody 上传键值对参数 , new FormBody.Builder();
RequestBody上传Json字符串和file对象,RequestBody.create()
MultipartBody上传file和键值对参数, new MultipartBody.Builder()

上传文件

自定义RequestBody实现上传文件

RequestBody requestBody = new RequestBody(
@Override
public MediaType contentType(){
return null;
}
@Override
public void writeTo(BuffereSink sink)Throw IOException(){
FileInputStream stream =new FileInputStream(new File(" "));
byte[] bytes=new byte[1024*8];
while(stream.read(bytes)!=-1){
sink.write(byte);
}
} 
);

Handler 详解

handler能否在子线程中运行 :
博文参考:https://blog.csdn.net/ErLiangCode/article/details/52117831
handlerThread博文参考:https://blog.csdn.net/lmj623565791/article/details/47079737

相关文章

网友评论

      本文标题:2018-12-24

      本文链接:https://www.haomeiwen.com/subject/ojeakqtx.html