編輯:關於Android編程
我們需要用以下方式來啟動service:
Intent intent = new Intent(); intent.setClass(MainActivity.this, MyService.class); bindService(intent,conn,BIND_AUTO_CREATE);
ServiceConnection conn = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { } @Override public void onServiceConnected(ComponentName name, IBinder service) { mService = ((MyService.MyBinder)service).getService(); } };
然後
我們去執行一些任務:
ImageView item_image = (ImageView)findViewById(R.id.item_image); item_image.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mService.setHandler(mHandler); mService.startCheckIfUpgrade(); } }); private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case GET_UPGRADE_INFORMATION_COMPLETE: System.out.println("=======notify need upgrade"); AlertDialog dialog = getCustomDialog(); dialog.show(); default: break; } } };
public class MyService extends Service { /** thread to execute background task*/ private Thread mThread = null; /** flag indicates if the service has been stopped*/ private boolean mStopFlag = false; private static final int GET_UPGRADE_INFORMATION_COMPLETE = 202; /** Handler to get notify from thread*/ private Handler mHandler = null; public void setHandler(Handler handler) { this.mHandler = handler; } @Override public boolean onUnbind(Intent intent) { System.out.println("====service onUnbind"); return super.onUnbind(intent); } public class MyBinder extends Binder { /** * 獲取當前Service的實例 * * @return */ public MyService getService() { return MyService.this; } } public void startCheckIfUpgrade() { if(mThread == null){ mThread = new UpgradeThread(); mThread.start(); } } @Override public IBinder onBind(Intent arg0) { System.out.println("====service onBind"); return new MyBinder(); } /** * @see android.app.Service#onCreate() */ @Override public void onCreate() { super.onCreate(); System.out.println("====service onCreate"); } private class UpgradeThread extends Thread{ /** * @see java.lang.Thread#run() */ @Override public void run() { super.run(); //在這裡寫去服務端查看是否需要升級的邏輯 try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } boolean ret = true; if(ret && !mStopFlag){ Message msg = new Message(); msg.what = GET_UPGRADE_INFORMATION_COMPLETE; mHandler.sendMessage(msg); } } } /* (non-Javadoc) * @see android.app.Service#onDestroy() */ @Override public void onDestroy() { System.out.println("====ondestroy"); mStopFlag = true; try { if (null != mThread && mThread.isAlive()) { try { mThread.stop(); } catch (Exception e) { } } mThread = null; } catch (Exception e) { e.printStackTrace(); } super.onDestroy(); System.gc(); } }
onUnbind和onDestroy
這個說明這個service的生命周期和activity是一致的
最近做的項目中,遇到個很棘手的問題: 客戶給我的數據是有限制的,因此,在返回某條具體頁面內容的時候,他只能給我一個html片段,裡面包含 文字,圖片以及附件的下載地址。如
安卓5.0Lollipop發布以來VectorDrawable作為安卓環境下的矢量化圖形的方式一直由於兼容性問題而很少被用到,由於只能用於5.0以上系統,導致現在多少安卓
Android異步更新UI-線程池-Future-Handler實例分析 前言: 我們在開發Android過程中,在處理耗時任務和UI交互的過程中,都會將耗時任務放到子
話說,從mta上報的數據上來看,我們的app出現了3起OOM(out of memery):java.lang.Throwable: java.lang.OutOfMem