当前位置:天才代写 > tutorial > 安卓教程 > Android进阶学习

Android进阶学习

2021-03-03 12:05 星期三 所属: 安卓教程 浏览:679

一、Android四大部件

1. Activity

生命期:

2. Service

生命期:

Service的生命期长,沒有操作界面,能够用于开发设计监管程序流程。

Service有二种启用方法:

根据Context.startService()启用Service,当调用者关掉时,Service不容易关掉,仅用根据Context.stopService()才可以关掉。

根据Context.bindService()启用Service,当调用者关掉时,Service也会关掉,关掉Service必须应用Context.unbindService()

3. Broadcast Receiver

Broadcast Receiver的生命期仅有十几秒,因此 不可以在onReceive()中做用时实际操作,不然会报ANR。也不可以开子进程,由于很有可能子进程还没有运作完Broadcast Receiver就结束了。应当根据推送Intent给Service来解决。

动态性申请注册广播节目信号接收器会伴随着申请注册的Activity关掉后关掉;静态数据的广播节目信号接收器就算app关掉,只需机器设备是打开的,接受到广播节目后仍可以开启。

4. Content Provider

 

二、Android 多线程

1. Handler承担线程间通信

Handler处在搭建它的主线任务程中,能够在子进程中根据post(Runnalbe)或是sendMessage(msg)来通告主线任务程升级UI。

Runnable: 实行handler.post(Runnalble),Handler将Runnable放进MessageQueue中,一旦从MessageQueue中取下,Runnable能够自启动它的Run方式来开展主线任务程的UI升级。

关键方式:

boolean post(Runnable r) :从消息队列中取下runnable后立即执行

boolean postAtTime(Runnable r, long uptimeMillis):从消息队列中取下runnable后,特定時间实行

boolean postDelayed(Runnable r, long delayMillis):从消息队列中取下runnable后,延迟时间一定時间实行

void removeCallbacks(Runnable r):从消息队列中删掉runnable

Message: 实行handler.sendMessage()或是msg.sendToTarget(),Handler会将msg放进MessageQueue中,但是msg不可以全自动实行,必须重写Handler的handleMessage方式,根据获得msg中的信息内容开展UI升级。可以用setData(Bundle),在其中Bundle能够根据putParcelable(String key, Parcelable value)和putSerializable(String key, Serializable value)传送目标。Message是一个final类不可以承继。有四个主要参数arg1,arg2,obj,what。

关键方式:

Message obtainMessage:从信息池里获得一个msg

boolean sendMessage:推送一个信息到消息队列,从消息队列中取下后立即执行

boolean sendMessageDelayed:推送一个信息到消息队列,从消息队列中取下后延迟时间实行

boolean removeMessage:从消息队列中清除一个未响应到信息

Handler使用方法,开一个子进程,随后在Handler.post(Runnable)的run方式里边升级UI

public class MainActivity extends Activity {

    ProgressDialog progressDialog;
    Handler handler;
    Button btn;
    TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn = (Button) findViewById(R.id.load_pic);
        tv = (TextView) findViewById(R.id.tv);

        progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("提醒");
        progressDialog.setMessage("正在加载,请稍后...");
        progressDialog.setCancelable(false);

        handler = new Handler();

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                progressDialog.show();

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(3000);
                            System.out.println("实行完用时实际操作");
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                tv.setText("实行结束");
                                progressDialog.dismiss();
                            }
                        });
                    }
                }).start();
            }
        });
    }
}

Message使用方法

书写1 message.obtain()不传主要参数,必须启用handler.sendMessage(msg)推送msg

public class MainActivity extends Activity {

    ProgressDialog progressDialog;
    Handler handler;
    Button btn;
    TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn = (Button) findViewById(R.id.load_pic);
        tv = (TextView) findViewById(R.id.tv);

        progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("提醒");
        progressDialog.setMessage("正在加载,请稍后...");
        progressDialog.setCancelable(false);

        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                Bundle bundle = msg.getData();
                tv.setText(bundle.getString("tv"));
                progressDialog.dismiss();
            }
        };

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                progressDialog.show();

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(3000);
                            System.out.println("实行完用时实际操作");
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        Message msg = Message.obtain();
                        Bundle bundle = new Bundle();
                        bundle.putString("tv", "实行结束");
                        msg.setData(bundle);
                        handler.sendMessage(msg);
                    }
                }).start();
            }
        });
    }
}

书写2 Message.obtain(handler)传一个handler主要参数,那样能够立即启用msg.sendToTarget()方式,由于在message內部有一个Handler特性的Target,等同于给target取值为Handler。

public class MainActivity extends Activity {

    ProgressDialog progressDialog;
    Handler handler;
    Button btn;
    TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn = (Button) findViewById(R.id.load_pic);
        tv = (TextView) findViewById(R.id.tv);

        progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("提醒");
        progressDialog.setMessage("正在加载,请稍后...");
        progressDialog.setCancelable(false);

        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                Bundle bundle = msg.getData();
                tv.setText(bundle.getString("tv"));
                progressDialog.dismiss();
            }
        };

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                progressDialog.show();

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(3000);
                            System.out.println("实行完用时实际操作");
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        Message msg = Message.obtain(handler);
                        Bundle bundle = new Bundle();                     bundle.putString("tv", "实行结束");
                        msg.setData(bundle);
                        msg.sendToTarget();
                    }
                }).start();
            }
        });
    }
}

2. AsyncTask类

 简易单一的多线程实际操作

关键方式:

1. onPreExecute():由UI进程启用,多线程实际操作逐渐实行以前启用

2. doInBackground():后台管理进程实行,解决用时实际操作

3. onProgressUpdate():升级UI进程,在doInBackground中启用publicProgress()就可以开展实行此方式

4. onPostExecute():UI进程启用,doInBackground()实行完以后启用

三、Android跨进程通信

运用四大部件完成:

1. Activity能够运行别的过程的Activity

在调用者Activity中应用Intent来运行被启用Activity,在被启用Activity中必须在AndroidManifest里边配备该Activity的action和data,action表明被启用Activity的字符串数组ID,data表明被启用Activity的浏览协议书Uri例如ok://abc。调用者Activity应用Intent(被启用Activity.Action, 被启用Activity.Uri)来运行别的过程中的Activity。被启用Activity中getIntent().getData()得到总体Uri, getIntent().getData().getHost()获得Uri行为主体也就是ok://后边的abc,能够依靠intent传送数据。自然假如在被启用Activity中setResult能够回到给调用者Activity数据信息,也就是数据通讯能够是双重的。

//调用者Activity里边完成
Intent intent = new Intent("com.wwwsealss.action.another", Uri.parse("ok://go and fight!"));
intent.putExtra("value", "Hello world!");
startActivity(intent);
finish(); 

//被调用者AndroidManifest里边申请注册
<activity android:name=".Main2Activity" >
    <intent-filter>
        <action android:name="com.wwwsealss.action.another" />
        <data android:scheme="ok" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

//被调用者Activity里边完成
if (getIntent().getData() != null) {
     //获得Uri行为主体一部分和intent中传送的数据信息
     tv.setText(getIntent().getData().getHost()   getIntent().getStringExtra("value"));
}

2. Content Provider向别的运用共享资源数据信息,而且容许别的运用对数据信息的增删

3. BroadCast 一个运用做为广播电台推送广播节目,别的运用做为录音机接受广播节目,但不可以和广播电台开展通讯,只有处于被动接受。

//根据intent推送广播节目,必须加Action
Intent intent = new Intent("com.wwwsealss.receivebroadcast");
intent.putExtra("value", "Hello World!");
sendBroadcast(intent);

//申请注册广播节目信号接收器
public class MyReceiver extends BroadcastReceiver {
    public MyReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String get = intent.getStringExtra("value");
        Toast.makeText(context, get, Toast.LENGTH_SHORT).show();
        Intent it = new Intent(context, MainActivity.class);
        //要是没有传到Activity的前后文必须加个标示
        it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(it);
    }
}

//在AndroidManifest中申请注册Receiver
<receiver
    android:name=".MyReceiver"
    android:enabled="true"
    android:exported="true" >
    <intent-filter>
        <action android:name="com.wwwsealss.receivebroadcast" />
    </intent-filter>
</receiver>

4. AIDL是进程间通信的服务项目,最先必须在服务器端建立AIDL文档(是一个插口,里边的抽象方法便是共享资源给手机客户端的),编译程序后会转化成相匹配的java文件。随后界定一个service,在这其中进行AIDL插口的完成类(完成其內部的抽象方法),随后重写service的onbind方式,让其回到该AIDL完成类的实例化目标,这样一来就完成了服务器端。随后将AIDL文档放进手机客户端工程项目中,留意AIDL所属文档的包名务必和服务器端中的包名一致,随后界定serviceConnecton用以在关联服务器端后获得其onbind方式回到的AIDL完成类的实例化目标(共享资源目标)。

//建立AIDL文档
interface IMyAidlInterface {
    String getValue();
}

//界定服务器端
public class MyService extends Service {

    //完成AIDL插口中的方式
    public class MyAidlInterface extends IMyAidlInterface.Stub {
        @Override
        public String getValue() throws RemoteException {
            return "从AIDL服务项目获得的值";
        }
    }

    public MyService() {
    }

    //关联该服务项目后回到AIDL插口的完成类的案例
    @Override
    public IBinder onBind(Intent intent) {
       return new MyAidlInterface();
    }
}

//在手机客户端中完成
btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
//根据intent Action关联服务器端 bindService(
new Intent("com.example.administrator.findjob.IMyAidlInterface"), serviceConnection, Context.BIND_AUTO_CREATE); } }); //界定AIDL插口用以获得推送回来的插口完成类 private IMyAidlInterface myAidlInterface = null; //创建一个服务项目联接目标用以获得服务器端onbind返回值 private ServiceConnection serviceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) {
//获得AIDL插口完成类目标 myAidlInterface
= IMyAidlInterface.Stub.asInterface(service); //启用AIDL插口完成类目标中的方式 try { Toast.makeText(MainActivity.this, myAidlInterface.getValue(), Toast.LENGTH_SHORT).show(); } catch (RemoteException e) { e.printStackTrace(); } } @Override public void onServiceDisconnected(ComponentName name) { } };

三、Android自定控制

 

三、Android系统软件架构

1. Linux Kernel 核心层

2. Hardware Abstraction Layer 硬件配置抽象性层

3. Libraries and Runtime 系统运行库层

4. Application Framework 应用软件架构层

5. Application 应用软件层

 

    关键字:

天才代写-代写联系方式