一些学习笔记和工作布置

我的工作是初步拟定项目计划

项目计划:

  13-14周完成基本UI构思、且进行市场调研(搜集情报、洞察市场、寻找机遇)

    14-16周完成UI细化、软件需求分析

  16-18周完成UI界面设计、将业务需求映射到软件详细设计层面
  7-8月学习Android开发并完成基本的软件架构
  9-10月进行软件测试,并与老师交流进行修正
  11-12月发布软件系统、为用户准备数据直到系统正常运转
 项目计划时间还是很紧张的于是赶紧在mooc上刷视频了,先了解一下大体框架再细分工作。
一些初学笔记,主要是仿着网课来学习的。
5.05
1、主线程中创建线程
      Handler handler=new Handler();
 
2  非主线程创建
      Looper.prepare();    //初始化Looper对象
      Handler handler1=new Handler(){
      public void handleMessage(Message msg){
              Log.i("Looper",String.valueOf(msg.what));
     }
 };
     Message m=handler1.obtainMessage();   //获取一个消息
     m.what=0x11;                          //设置Message的what属性值
     handler1.sendMessage(m);              //发送消息
     Looper.loop();                        //启动Looper


Message类的简介




5.07
service学习
2种服务
  1、Started(启动)
  2、Bound(绑定);


  重要方法
   onStartCommand();
   onBind();
   onCreate();
   onDestroy();
   组建调用startService()方法启动服务(onStartCommand()方法被调用)
   组件调用bindService()方法启动服务(onStartCommand()方法不被调用)
   需要在配置文件里面声明全部的Service,在Application中添加<Sercice>
   <Service  android:enabled="true"   //是否能被系统实例化
             android:exported="true"   //其他应用程序是否能调用服务
             android:icon="drawable resource"  //服务的图标
             android:label=""       //服务名称
             android:name=""        //里面写包名 必须指定
             android:permission=""   //实体必须包含的权限名称
             android:process="" >  //服务运行的进程名称
             
   </Service>
1、创建启动式服务
   1、Service :
   2、IntendService :
      IntentService:这是Service类的子类,它每次使用一个工作线程来处理全部启动请求,在不必要同时处理多个请求时候,这是最佳选择
   仅需要实现onHandleIntent()方法  该方法接受每次接受启动请求的Intent以便完成后台任务
  1、通过继承Service类
  2、通过继承IntentService类
     这里面有个onHandleIntent(Intent intent ){}需要从写


5.09

2、绑定式启动服务
  1、继承Binder类  这个地方一定要注意 在AndroidMainfest.xml文件里面的<application></application>里面加上<service andorid:name=""/>
  相关代码
   如果服务仅用于本地应用程序并且不必垮进程工作,开发人员可以实现自己的Binder类作为客户端提供访问服务端公共方法。
package com.example.think.binder;


import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;


import java.util.Random;


/**
 * Created by Think on 2015/8/30.
 */
public class LocalService extends Service{
    private final IBinder binder=new LocalBinder();
    private final Random generator=new Random();
    class LocalBinder extends Binder{
        public LocalService getService(){
            return LocalService.this;
        }
    }
    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }
    public int getNum(){
        return 520;
    }
}


package com.example.think.binder;


import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


/**
 * Created by Think on 2015/8/30.
 */
public class MainActivity extends Activity {
    private LocalService localService;
    private boolean boo=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Button button=(Button)findViewById(R.id.showTime);
        final Button button1=(Button)findViewById(R.id.button2);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                Toast.makeText(MainActivity.this, "success", Toast.LENGTH_SHORT).show();
            }
        });
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.print("boo"+boo);
                if(boo){
                    if(localService==null){
                        System.out.println("localService is null");
                    }else{
                        System.out.println("localService is not null");
                        int num=localService.getNum();
                        System.out.println("num"+num);
                        Toast.makeText(MainActivity.this, "num" + num, Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }


    @Override
    protected void onStop() {
        super.onStop();
        if(boo){
            unbindService(connection);
            boo=false;
        }
    }


    @Override
    protected void onStart() {
        super.onStart();
        Intent intent=new Intent(this,LocalService.class);
        bindService(intent,connection, Context.BIND_AUTO_CREATE);
    }
    private ServiceConnection connection=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            LocalService.LocalBinder binder=(LocalService.LocalBinder)service;
            localService=binder.getService();
            boo=true;
            System.out.println("has  go to ServiceConnetion");
        }


        @Override
        public void onServiceDisconnected(ComponentName name) {
            boo=false;
        }
    };
}


2 使用Messenger类需要服务与远程进程通信,可以通过Messenger来为服务提供接口,该技术允许不使用ALDL执行进程间通信(IPC)
Handler 是属于 android.os.Handler这个包里面的 要注意
  package com.example.think.binder;


import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.support.annotation.Nullable;
import android.widget.Toast;


import java.util.logging.Handler;


/**
 * Created by Think on 2015/8/30.
 */
public class MessengerService extends Service{
    static final int HELLO_WORD=1;
    class IncomingHandler extends android.os.Handler{
        @Override
        public void handleMessage(Message msg) {
            switch(msg.what){
                case HELLO_WORD:
                    Toast.makeText(getApplicationContext(),"HELLO WORD",Toast.LENGTH_SHORT).show();
                    break;
                default:
                    super.handleMessage(msg);
            }
        }
    }
   final Messenger messenger=new Messenger(new IncomingHandler());
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Toast.makeText(getApplicationContext(),"Binding",Toast.LENGTH_SHORT).show();
        return messenger.getBinder();
    }
}




package com.example.think.binder;


import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


/**
 * Created by Think on 2015/8/30.
 */
public class MainActivity extends Activity {
//    private LocalService localService;
//    private boolean boo=false;
    private Messenger messenger=null;
    private boolean bound=false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Button button=(Button)findViewById(R.id.showTime);
        final Button button1=(Button)findViewById(R.id.button2);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 if(!bound)return;
                Message message=Message.obtain(null,MessengerService.HELLO_WORD,0,0);
                try {
                    messenger.send(message);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });
//        button.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                System.out.print("boo"+boo);
//                if(boo){
//                    if(localService==null){
//                        System.out.println("localService is null");
//                    }else{
//                        System.out.println("localService is not null");
//                        int num=localService.getNum();
//                        System.out.println("num" + num);
//                        Toast.makeText(MainActivity.this, "num" + num, Toast.LENGTH_SHORT).show();
//                    }
//                }
//            }
//        });
    }


    @Override
    protected void onStop() {
        super.onStop();
//        if(boo){
//            unbindService(connection);
//            boo=false;
//        }
        if(bound){
            unbindService(connection1);
            bound=false;
        }
    }
    @Override
    protected void onStart() {
        super.onStart();
//        Intent intent=new Intent(this,LocalService.class);
//        if(boo) bindService(intent,connection, Context.BIND_AUTO_CREATE);
          bindService(new Intent(this,MessengerService.class),connection1,Context.BIND_AUTO_CREATE);
    }
//    private ServiceConnection connection=new ServiceConnection() {
//        @Override
//        public void onServiceConnected(ComponentName name, IBinder service) {
//            LocalService.LocalBinder binder=(LocalService.LocalBinder)service;
//            localService=binder.getService();
//            boo=true;
//            System.out.println("has  go to ServiceConnetion");
//        }
//        @Override
//        public void onServiceDisconnected(ComponentName name) {
//            boo=false;
//        }
//    };
    private ServiceConnection connection1=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            messenger=new Messenger(service);
            bound=true;
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {
            messenger=null;
            bound=false;
        }
    };
}


onStartCommand;    已经取代了onStart();
原文地址:https://www.cnblogs.com/yangyang0717/p/5506976.html