geekband android #5 第九周分享
16lz
2022-03-24
1.什么是进程?
系统进行资源分配以及调度的基本单位,进程是线程的容器。
android:process创建一个进程。
android:":push",附加一个进程,私有进程。
android:"push" ,两个独立进程,全局进程。
进程有优先等级:前台进程,可见进程,服务进程,后台进程,空进程。
什么情况下使用多进程?由于进程之间相互独立,而线程不行,因此,当需要满足相互之间不影响,更安全的时候,就需要使用到多进程。而非多线程。
使用多进程时需要注意什么? 1.多进程之间内存不能共享。
2.没创建一个进程就就要跑一次Application的onCreate,因此要注意Activity的管理。
3.调试的时候去掉AndroidManifest.xml文件中Activity的android:process标签,这样保证调试状态下是在同一进程中,堆栈信息是连贯的,在调试 完成后记得复原该属性;通过打印进行调试,但这种效率比较低。
2.多进程之间的通信IPC。
IPC:inter precess communication messager:单线程,多进程的时候使用。 AIDL:多进程多线程,较为复杂的时候使用。 messager用法:(该部分引用tukangzheng的CSDN)import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.Menu;import android.view.View;import android.widget.Button;import android.widget.TextView;public class MessageActivity extends Activity {private Button button01, button02, button03, button04, button05;private TextView textView;private Handler handler = new Handler(){@Overridepublic void handleMessage(Message msg) {// TODO Auto-generated method stubif(msg.what == 3 || msg.what == 5){textView.setText("what=" + msg.what + ", 这是一个空消息");}else{textView.setText("what=" + msg.what + "," + msg.obj.toString());}}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);button01 = (Button)findViewById(R.id.button01);button02 = (Button)findViewById(R.id.button02);button03 = (Button)findViewById(R.id.button03);button04 = (Button)findViewById(R.id.button04);button05 = (Button)findViewById(R.id.button05);textView = (TextView)findViewById(R.id.textView);button01.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubnew Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubMessage message = Message.obtain();message.what = 1;message.obj = "使用Message.Obtain+Hander.sendMessage()发送消息";handler.sendMessage(message);}}).start();}});button02.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubnew Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubMessage message = Message.obtain(handler);message.what = 2;message.obj = "使用Message.sendToTarget发送消息";message.sendToTarget();}}).start();}});button03.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubnew Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubhandler.sendEmptyMessage(3);}}).start();}});button04.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubnew Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubMessage message = Message.obtain();message.what = 4;message.obj = "使用Message.Obtain+Hander.sendMessage()发送延迟消息";handler.sendMessageDelayed(message, 3000);}}).start();}});button05.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubnew Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubhandler.sendEmptyMessageAtTime(5, 3000);}}).start();}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.message, menu);return true;}}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <Button android:id="@+id/button01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/text01"/> <Button android:id="@+id/button02" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/text02"/> <Button android:id="@+id/button03" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/text03"/> <Button android:id="@+id/button04" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/text04"/> <Button android:id="@+id/button05" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/text05"/> <TextView android:id="@+id/textView" android:layout_width="fill_parent" android:layout_height="wrap_content"/></LinearLayout>
<?xml version="1.0" encoding="utf-8"?><resources> <string name="app_name">MessageActivity</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="text01">用Handler.sendMessage发送消息</string><string name="text02">用Message.sendToTarget发送消息</string><string name="text03">发送空消息</string><string name="text04">用Handler.sendMessage发送延迟消息</string><string name="text05">发送延迟空消息</string></resources>
AIDL:android interface difinition lauguage,其用法为:这部分我也没弄太懂,建议参考http://blog.csdn.net/stonecao/article/details/6425019
3.深入Service.
service可以分为:Local和Remote ; 前台和后台 ; Start和Bind 。 关于Notification方法:Create a Notification Builder Define the Notification's Action Set the Notification 's Click Behavior Issue the Notification 先new一个notification在new一个Pending Intent.
1 )得到 NotificationManager :String ns = Context.NOTIFICATION_SERVICE;NotificationManager mNotificationManager = (NotificationManager) getSystemService( ns );2 )创建一个新的 Notification 对象:Notification notification = new Notification();notification.icon = R.drawable.notification_icon;// 也可以使用稍微复杂一些的方式创建 Notification :int icon = R.drawable.notification_icon; 通知图标CharSequence tickerText = "Hello"; // 状态栏 (Status Bar) 显示的通知文本提示long when = System.currentTimeMillis(); // 通知产生的时间,会在通知信息里显示Notification notification = new Notification(icon, tickerText, when) ;3 )填充 Notification 的各个属性:Context context = getApplicationContext();CharSequence contentTitle = "My notification";CharSequence contentText = "Hello World!";Intent notificationIntent = new Intent(this, MyClass.class);PendingIntent contentIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);Notification 提供了丰富的手机提示方式:a) 在状态栏 (Status Bar) 显示的通知文本提示,如:notification.tickerText = "hello";b) 发出提示音,如:notification.defaults |= Notification.DEFAULT_SOUND;notification.sound = Uri.parse("file:/ sdcard /notification/ringer.mp3");notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");c) 手机振动,如:notification.defaults |= Notification.DEFAULT_VIBRATE;long[] vibrate = {0,100,200,300};notification.vibrate = vibrate ;d)LED 灯闪烁,如:notification.defaults |= Notification.DEFAULT_LIGHTS;notification.ledARGB = 0xff00ff00;notification.ledOnMS = 300;notification.ledOffMS = 1000;notification.flags |= Notification.FLAG_SHOW_LIGHTS;e) 添加 remote view通过 RemoteViews 设置 notification 中 View 的属性notification.contentView = new RemoteViews(getApplication().getPackageName(), R.layout.custom_dialog);notification.contentView.setProgressBar(R.id.pb, 100, 0, false);notification.contentView.setTextViewText(R.id.tv, " 进度 " + _progress+ "%");4 )发送通知:private static final int ID_NOTIFICATION = 1;mNotificationManager.notify(ID_NOTIFICATION, notification);
更多相关文章
- Android(安卓)EditText的使用
- Android(安卓)Sqlite 数据库—基础篇
- android学习五(android中基本控件的使用)
- (android)如何破解多盟广告sdk (一)
- android绘图Paint.setXfermode()和Canvas.saveLayer()方法的作用
- 一个用于Android的Web服务器
- 箭头函数的基础使用
- NPM 和webpack 的基础使用
- Python list sort方法的具体使用