全局获取Context
private class MyApplication extends Applocation{
private static Context mcontext;
@Override
public void onCreate(){
mcontext=getApplicationContext();
}
public static Context getContext()
{
return mcontext
}
}
在清单文件中在application中添加name标签:包名+类名
有冲突时将context传递LitePalApplication.initialize(mcontext);
使用Intent传递对象
//Serializable方式
是对象实现Serializable接口,将对象进行序列化
//Parcellable方式
实现Parcellable接口,重写describeContents()和writeToParcel两个方法
public class Student implements Parcelable {
private String name;
private int age;
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(age);
}
@Override
public int describeContents() {
return 0;
}
public static final Creator<Student> CREATOR = new Creator<Student>() {
@Override
public Student createFromParcel(Parcel in) {
Student student=new Student();
student.name=in.readString();
student.age=in.readInt();
return student;
}
@Override
public Student[] newArray(int size) {
return new Student[size];
}
};
}
定制自己的日志工具
public class LogUtil {
public static final int VERBOSE=1;
public static final int DEBUG=2;
public static final int INFO=3;
public static final int WARN=4;
public static final int ERROR=5;
public static final int NOTHING=6;
public static int level=VERBOSE;
public static void v(String tag,String msg){
if (level<=VERBOSE){
Log.v(tag,msg);
}
}
public static void d(String tag,String msg){
if (level<=DEBUG){
Log.d(tag,msg);
}
}
public static void i(String tag,String msg){
if (level<=INFO){
Log.i(tag,msg);
}
}
public static void w(String tag,String msg){
if (level<=WARN){
Log.w(tag,msg);
}
}
public static void e(String tag,String msg){
if (level<=ERROR){
Log.e(tag,msg);
}
}
}
//通过修改level的值来修改打印日志
创建定时任务
##Alarm机制
AlarmManager manager=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
调用AlarmManager的set方法
AlarmManager alarmManager= (AlarmManager) getSystemService(Context.ALARM_SERVICE);
long triggertime= SystemClock.elapsedRealtime()+10*100;
Intent intent1=new Intent(this,MyService.class);
PendingIntent pendingIntent=PendingIntent.getService(this,0,intent1,0);
alarmManager.set(AlarmManager.RTC_WAKEUP,triggertime, pendingIntent);
//由于现在对于cpu优化Alarm机制不在准确
可以调用xactExact来执行