今天还好,给姥爷庆祝完生日回家,接着奔向申通去拿快递,回家之后又开始拉网线,鼓捣网络机顶盒,忙忙活活一直到晚上八点才开始正式学习,

今天晚上主要学习了服务相关的知识,由于在进行代码实践时,已经将知识点和重要注意写入代码,所以下面直接贴代码:

一、首先是安卓线程的理解

package com.example.androidthreadtest;
 /*
  * 服务:是安卓实现服务后台运行的解决方案,它非常适合去执行那些不需要和
  * 用户交互而需要长期运行的任务。
  * 服务并不是运行在一个独立进程之中,而是依赖创建服务所在的应用程序进程。当某个应用进程
  * 被杀掉时,所有依赖该进程的服务也会停止运行。
  */
 /**
  * 解析异步消息处理机制
  * Android中的异步消息处理主要由四个部分组成:
  * Message、handler、MessageQueue和Looper
  * Messsge:是指在线程之间传递的消息
  * Handler:用于发布和处理消息
  * MessageQueue:用于存放所有通过 Handler发送的消息
  * Looper:每个MessageQueue的管家。   
  */
 import android.support.v7.app.ActionBarActivity;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.Button;
 import android.widget.TextView;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.Message;


 public class MainActivity extends ActionBarActivity implements OnClickListener{
private static final int UPDATA_TEXT=1;
private TextView text;
private Button changeText;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         //获取实例之后才能进行之后的关于TextView的操作
         text=(TextView)findViewById(R.id.text);
         changeText=(Button)findViewById(R.id.change_text);
         changeText.setOnClickListener(this);
     }
   //匿名内部类
   private Handler handler=new Handler(){
   public void handleMessage(Message message)
   {
   switch (message.what) {
   case UPDATA_TEXT:
   //在这里进行UI操作
   text.setText("Nice to meet you");
   break;


   default:
   break;
   }
   }
   };
     public void onClick(View v)
     {
     switch (v.getId()) {
case R.id.change_text:
//匿名内部类
new Thread(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
/**
* 创建Message对象,并通过handler将条消息发送出去
*/
Message message=new Message();
message.what=UPDATA_TEXT;
handler.sendMessage(message);//将Message对象发送出去
}
}).start();
break;


default:
break;
}
     }
 }

上述代码成功解决了子线程不能更新UI的问题

二、服务的基本用法

public class MyService extends Service {
private DownloadBinder mBinder=new DownloadBinder();
@Override
//服务创建时调用
public void onCreate() {
// TODO Auto-generated method stub

super.onCreate();
Notification notification=new Notification(R.drawable.ic_launcher, "Notification comes",System.currentTimeMillis()
);
Intent notificationIntent=new Intent(this,MainActivity.class);
//这里指的是又会重新回到主活动
PendingIntent pendingIntent=PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, "This is Title", "This is content", pendingIntent);
notification.defaults=Notification.DEFAULT_ALL;
startForeground(1, notification);
Log.d("MyServicer","onCreate executed");
}


@Override
//服务销毁的时候调用
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.d("MyServicer","onDestroy executed");
}


@Override
//每次执行服务的时候调用
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.d("MyServicer","onStartCommand executed");
return super.onStartCommand(intent, flags, startId);
}


///唯一的一个抽象的方法,必须实现
@Override
public IBinder onBind(Intent arg0) {
return mBinder;
}
class DownloadBinder extends Binder{
public void startDownload()
{
Log.d("MyService", "startDownload executed");
}
public int getProgress()
{
Log.d("MyService", "getProgress executed");
return 0;
}
}

注意:不要忘记注册服务!!!<Service/>

三、活动与服务进行通信

private ServiceConnection connection=new ServiceConnection() {

@Override
//活动与服务解除绑定时调用
public void onServiceDisconnected(ComponentName arg0) {

}

@Override
//活动与服务成功绑定时调用
public void onServiceConnected(ComponentName name, IBinder service) {
downloadBinder=(MyService.DownloadBinder)service;
downloadBinder.startDownload();
downloadBinder.getProgress();
}
};
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         startService=(Button)findViewById(R.id.start_service);
         stopService=(Button)findViewById(R.id.stop_service);
         bindService=(Button)findViewById(R.id.bind_service);
         unbindService=(Button)findViewById(R.id.unbind_service);
         startIntentService=(Button)findViewById(R.id.start_intent_service);
         startService.setOnClickListener(this);
         stopService.setOnClickListener(this);
         bindService.setOnClickListener(this);
         unbindService.setOnClickListener(this);
         startIntentService.setOnClickListener(this);
     }
     public void onClick(View v)
     {
     switch (v.getId()) {
case R.id.start_service:
Intent startIntent = new Intent(this,MyService.class);
startService(startIntent);//启动活动
break;
case R.id.stop_service:
Intent stopIntent=new Intent(this,MyService.class);
stopService(stopIntent);
break;
case R.id.bind_service:
Intent bindiIntent=new Intent(this,MyService.class);
//第一个参数是Intent。第二个参数是前面创建的ServiceConnection实例,里面有绑定成功
//或者解除绑定后需要执行的方法
//第三个参数是一个标志位,这里传入BIND_AUTO_CREATE表示活动和服务进行绑定后自动创建服务
//这使得MyService中的onCreate方法得到执行,也就是说服务只是创建了,并没有执行,我们只是在
//活动里调用了服务的方法
bindService(bindiIntent,connection,BIND_AUTO_CREATE);//绑定服务
break;
case R.id.unbind_service:
               unbindService(connection);//解绑服务
               break;}
}

通过上面代码实现了活动让服务干什么,服务就去干什么

四、使用前台服务

Notification notification=new Notification(R.drawable.ic_launcher, "Notification comes",System.currentTimeMillis()
);
Intent notificationIntent=new Intent(this,MainActivity.class);
//这里指的是又会重新回到主活动
PendingIntent pendingIntent=PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, "This is Title", "This is content", pendingIntent);
notification.defaults=Notification.DEFAULT_ALL;
startForeground(1, notification);//与创建通知的区分点
Log.d("MyServicer","onCreate executed");

五、IntentService

为了避免直接在服务里去处理一些耗时的逻辑,我们采用android多线程编程技术,在 服务里去开启一个子线程,在这里去处理一些耗时的逻辑。
为了可以简单的创建一个异步的、会自动停止的服务,android专门提供了一个IntentService 类。 我们首先要提供一个无参构造函数,并且在其内部调用父类的有参构造函数,然后要在子类中去实现onHandleIntent()这个抽象方法,在这个方法里去处理一些具体的逻辑,而且不用担心ANR问题
 

不知不觉已经坚持了十天了,以后更要加油,明天又是新的一天,晚安!!!

public class MyIntentService extends IntentService {
public MyIntentService() {
super("MyIntentService");// 调用父类的有参构造函数
}
@Override
protected void onHandleIntent(Intent arg0) {
// 打印当前线程的id
Log.d("MyIntentService", "Thread id is"
+ Thread.currentThread().getId());
}

public void onDestroy() {
super.onDestroy();
Log.d("MyIntentService", "onDestroy executed");
}
 }

寄语:不要要你的脾气大于你的本事!!!