第十三章-- 一些技巧工具

2017-03-21  本文已影响13人  dong_hui

1.全局获取context的技巧

public class MyApplication extends Application {
      
    private static Context context;

    @Override
    public void onCreate() {
        super.onCreate();
        context=getApplicationContext();
    }

    public static Context getContext() {
        return context;
    }
}

2.使用Intent传递对象

-Serializable序列化:

public class Person implements Serializable {
      private String  name;
      private int   age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}


//传递
   Person   person=new Person();
        person.setName("huazai");
        person.setAge(18);
        
        Intent  intent=new Intent(……);
        intent.putExtra("persong_data",person);

//获取
  Person  person1= (Person) getIntent().getSerializableExtra("persong_data");
  public class Song  implements Parcelable {
    private String  name;
    private String  singer;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSinger() {
        return singer;
    }

    public void setSinger(String singer) {
        this.singer = singer;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
           parcel.writeString(name);        //此处写和下面的读  必须顺序一致
           parcel.writeString(singer);

    }

    public static  final  Parcelable.Creator<Song>  CREATOR=new Parcelable.Creator<Song>(){
        @Override
        public Song createFromParcel(Parcel parcel) {
            Song  song=new Song();
            song.name=parcel.readString();
            song.singer=parcel.readString();
            return song;
        }

        @Override
        public Song[] newArray(int i) {
            return new Song[i];
        }
    };

}

//使用

   //传递
        Song  song=new Song();
        song.setName("隐形人");
        song.setName("孙燕姿");
        Intent  intent=new Intent();
        intent.putExtra("songInfo",person);
        
   //获取
        Song  song1=(Song) getIntent().getParcelableExtra("songInfo");

3. 创建定时任务

public class LongRunningService  extends Service {
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                  //执行具体逻辑
                
            }
        }).start();


        AlarmManager manager= (AlarmManager) getSystemService(MyApplication.getContext().ALARM_SERVICE);
         int anhour=60*60*1000;
        long triggerAtTime= SystemClock.elapsedRealtime()+anhour;
        Intent  intent1=new Intent(this,LongRunningService.class);
        PendingIntent  pi=PendingIntent.getService(this,0,intent1,0);
   //set不够精确,是系统在耗电方面的优化; setExact()会保证任务精确的执行
        manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,triggerAtTime,pi);
        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

4. Doze模式

5.多窗口模式

    <application
        android:resizeableActivity="false"
      ……

6. Lambda表达式

上一篇下一篇

猜你喜欢

热点阅读