一、建立工程
1.建立一个空白的工程,名称为ModelApp。
2.在main-java下新建package,起名com.jiaweiqiang.tools,用于放置通用工具类。
二、建立得到全局变量的Application类
1.在tools中新建MyApplication类,用于存储全局context和其他程序需要的通用变量。
public class MyApplication extends Application { private static Context context; private static LocalBroadcastManager localBroadcastManager; @Override public void onCreate() { super.onCreate(); context = getApplicationContext(); localBroadcastManager = LocalBroadcastManager.getInstance(context); } /** * 得到全局的一个Context * @return 全局的Context */ public static Context getContext(){ return context; } /** * 公用的本地广播的管理器,可以将广播限制在本软件接收 * @return 公用的管理器 */ public static LocalBroadcastManager getLocalBroadcastManager(){ return localBroadcastManager; } }
2.在AndroManifest.xml文件中注册自定义的MyApplication类。
<application android:name="com.jiaweiqiang.tools.MyApplication" > </application>
三、统一管理活动
1.建立一个自定义的所有活动的父类,自动将所有活动添加到上面的ActivityCollector中统一管理。
/** * Created by jiaweiqiang on 2017/12/30. * <p>所有活动的父类,互动创建时增加了写入功能,活动销毁时,将活动移除</p> */ public class BaseActivity extends AppCompatActivity { public static List<Activity> activities = new ArrayList<Activity>(); public static void addActivity(Activity activity){ activities.add(activity); } public static void removeActivity(Activity activity){ activities.remove(activity); } public static void finishAll(){ for(Activity a : activities){ if(!a.isFinishing()){ a.finish(); } } } @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); addActivity(this); } @Override protected void onDestroy() { super.onDestroy(); removeActivity(this); } }
2.后续新建的活动全部要继承自BaseActivity。如果再某个活动要结束全部活动,直接调用静态方法即可。
四、定时服务工具
1.新建TimerService服务
/** * 定时服务,启动后,定时发送广播,完成定时功能 * <p>提供精确定时和非精确定时</p> */ public class TimerService extends Service { /** * 定时进行广播,相当于全局定时器 */ public final static String TIMER_DISPLAY = "cou.jiaweiqiang.tools.timer"; private static AlarmManager manager; private static Intent timeIntent; private static PendingIntent timePendingIntent; private static Handler sHandler ; private static Runnable sRunnable; public TimerService() { } @Override public int onStartCommand(Intent intent, int flags, int startId) { timerBroadcast(); return super.onStartCommand(intent, flags, startId); } /** * 定时进行广播,相当于全局定时器 */ private void timerBroadcast(){ Intent intent = new Intent(TIMER_DISPLAY); MyApplication.getLocalBroadcastManager().sendBroadcast(intent); } /** * 开启定时服务,每次定时开启,可以在传入的活动或者服务的startCommond中重复调用执行代码 * @param context 使用定时服务的上下文 * @param interval 定时的间隔 */ public static void startAlarmByAlarmManager(Context context,int interval){ manager = (AlarmManager)context.getSystemService(ALARM_SERVICE); int timer = interval; long triggerAtTimes = System.currentTimeMillis()+timer; timeIntent = new Intent(context,TimerService.class); timePendingIntent = PendingIntent.getService(context,0,timeIntent,0); manager.setRepeating(AlarmManager.RTC_WAKEUP,triggerAtTimes,timer,timePendingIntent); } public static void stopAlarmByAlarmManager(){ manager.cancel(timePendingIntent); } /** * 定时工具,采用handler方法开启定时,一定时间间隔定时唤醒服务 * @param context 需要开启的服务的上下文 * @param interval 整数的时间间隔,以毫秒计算 */ public static void startAlarmByHandler(Context context, int interval){ sHandler = new Handler() ; sRunnable = new Runnable() { @Override public void run() { TimerService.startService(context); sHandler.postDelayed(this,interval); } }; sHandler.postDelayed(sRunnable,interval); } /** * 停止以handler方法开启的定时操作 */ public static void stopAlarmByHandler(){ sHandler.removeCallbacks(sRunnable); } /** * 启动本服务的启动函数 * @param context 启动服务的上下文 */ public static void startService(Context context){ Intent intent = new Intent(context,TimerService.class); context.startService(intent); } /** * 停止本服务的函数 * @param context 要停止服务的上下文 */ public static void stopService(Context context){ Intent intent = new Intent(context,TimerService.class); context.stopService(intent); } /** * 为某个广播接收器注册接收定时广播的功能 * @param mReceiver 要接收广播的接收器 */ public static void registerTimerReceiver(BroadcastReceiver mReceiver){ IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(TimerService.TIMER_DISPLAY); MyApplication.getLocalBroadcastManager().registerReceiver(mReceiver,intentFilter); } /** * 为注册定时广播的接收器解除注册 * @param mReceiver */ public static void unregisterTimerReceiver(BroadcastReceiver mReceiver){ MyApplication.getLocalBroadcastManager().unregisterReceiver(mReceiver); } @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. throw new UnsupportedOperationException("Not yet implemented"); } }
2.在需要定时服务的活动或者服务中,调用API进行注册并使用。
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); TimerService.registerTimerReceiver(mReceiver); } private BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { num++; mtxt.setText(String.valueOf(num)); } }; Override protected void onDestroy() { super.onDestroy(); TimerService.unregisterTimerReceiver(mReceiver); }
五、持久化存储
1.建立以程序名称为名称的键值对存储文件。并将存取函数用静态函数进行封装。
/** * Created by jiaweiqiang on 2017/12/31. * <p>程序公用的一个数据存储集合,是以程序名称命名的一个持久化存储文件的封装</p> * <p>其中主要存储一些程序运行的设置参数</p> */ public class DataShare { private static SharedPreferences pref; private static SharedPreferences.Editor editor; /** * 表示初次写入的标志位,如果是false,则表示从未写入,一旦写入,就变为true */ private final static String FIRST_FLAG = "firstSetupFlag"; public final static String ONCE_FLAG = "ifOnceMeasureFlag"; public final static String SERIAL_MEASURE_TIMES = "serialMeasureTimes"; //初始化程序数据,如果从未写入,则初次写入数据 public DataShare(){ //采用默认名称的存储 pref = PreferenceManager.getDefaultSharedPreferences(MyApplication.getContext()); editor = pref.edit(); if(pref.getBoolean(FIRST_FLAG,false)== false){ initPara(); }else { editor.putBoolean(ONCE_FLAG,true); editor.apply(); } } private void initPara(){ editor.putBoolean(FIRST_FLAG,true); editor.putInt(SERIAL_MEASURE_TIMES,3); editor.putBoolean(ONCE_FLAG,true); editor.apply(); } /** * 从程序的公用存储中得到制定名称的一个int值,如果此名称对应的值不存在,则返回-1 * @param key int值对应的键名 * @return 存储的int值 */ public static int getInt(String key){ return pref.getInt(key,-1); } public static void putInt(String key,int data){ editor.putInt(key,data); editor.apply(); } /** * 从程序的公用存储中得到制定名称的一个float值,如果此名称对应的值不存在,则返回-1 * @param key 值对应的键名 * @return 存储的float值 */ public static float getFloat(String key){ return pref.getFloat(key,-1); } public static void putFloat(String key,float data){ editor.putFloat(key,data); editor.apply(); } public static void putBoolean(String key,boolean data){ editor.putBoolean(key,data); editor.apply(); } public static boolean getBoolean(String key){ return pref.getBoolean(key,true); } }
2.在MyApplication中初始化此类。
private static DataShare dataShare; @Override public void onCreate() { super.onCreate(); context = getApplicationContext(); localBroadcastManager = LocalBroadcastManager.getInstance(context); dataShare = new DataShare(); }
六、定制日志工具
建立logUtil类
/** * Created by jiaweiqiang on 2018/1/21. * <p>可以通过将level设置不同的常量值,控制打印的范围</p> */ 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); } } }