編輯:關於Android編程
擴展Service 這是所有服務的基類。擴展這個類的時候,特別重要的一點是,需要創建一個新的線程來做服務任務,因為service默認是運行在你的主線程(UI線程)中的,它會使你的主線程運行緩慢。
擴展IntentService 這是一個service的子類,他可以在一個工作線程中處理所有的啟動請求。如果你不需要同一時刻出來所有的服務請求,使用IntentService是一個很好的選擇。你需要做的僅僅是實現onHandlerIntent()方法,通過這個函數處理接受的每一個啟動請求。
1.創建一個獨立於主線程的工作線程,用於執行通過onStartCommand()傳來的所有的intent。 2.創建一個工作隊列,將接受的所有intent一個一個的傳給onHandlerIntent(),所以同一時間內你只處理一個intent,不用擔心多線程的問題。 3.當所有的請求都處理完後,停止服務,所以你不需要手動調用stopSelf()。 4.提供onBind()函數的默認實現,返回null 5.提供onStartCommand()函數的默認實現,它把intent發送到工作隊列中去,然後工作隊列再發送到你的onHandlerIntent()函數中。
public class HelloIntentService extends IntentService {
/**
* A constructor is required, and must call the superIntentService(String)
* constructor with a name for the worker thread.
*/
public HelloIntentService() {
super("HelloIntentService");
}
/**
* The IntentService calls this method from the default worker thread with
* the intent that started the service. When this method returns, IntentService
* stops the service, as appropriate.
*/
@Override
protected void onHandleIntent(Intent intent) {
// Normally we would do some work here, like download a file.
// For our sample, we just sleep for 5 seconds.
long endTime = System.currentTimeMillis() + 5*1000;
while (System.currentTimeMillis() < endTime) {
synchronized (this) {
try {
wait(endTime - System.currentTimeMillis());
} catch (Exception e) {
}
}
}
}
}
@Override有一點例外的是,如果你需要其他組件綁定服務,那麼你的onBind函數不需要調用父類的onBind。
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
return super.onStartCommand(intent,flags,startId);
}
public class HelloService extends Service {如同你看到的,比IntentService的例子多了好多代碼。
private Looper mServiceLooper;
private ServiceHandler mServiceHandler;
// Handler that receives messages from the thread
private final class ServiceHandler extends Handler {
public ServiceHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
// Normally we would do some work here, like download a file.
// For our sample, we just sleep for 5 seconds.
long endTime = System.currentTimeMillis() + 5*1000;
while (System.currentTimeMillis() < endTime) {
synchronized (this) {
try {
wait(endTime - System.currentTimeMillis());
} catch (Exception e) {
}
}
}
// Stop the service using the startId, so that we don't stop
// the service in the middle of handling another job
stopSelf(msg.arg1);
}
}
@Override
public void onCreate() {
// Start up the thread running the service. Note that we create a
// separate thread because the service normally runs in the process's
// main thread, which we don't want to block. We also make it
// background priority so CPU-intensive work will not disrupt our UI.
HandlerThread thread = new HandlerThread("ServiceStartArguments",
Process.THREAD_PRIORITY_BACKGROUND);
thread.start();
// Get the HandlerThread's Looper and use it for our Handler
mServiceLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
// For each start request, send a message to start a job and deliver the
// start ID so we know which request we're stopping when we finish the job
Message msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
mServiceHandler.sendMessage(msg);
// If we get killed, after returning from here, restart
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
// We don't provide binding, so return null
return null;
}
@Override
public void onDestroy() {
Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show();
}
}
START_NOT_STICKY 在onStartCommand()返回後,系統殺死服務,服務將不會重啟,除非有pending intents(目前筆者還不懂什麼是pending Intents)要投遞。這比較適合非常容易就可以重新啟動未完成任務的情況。
START_STICKY 在系統殺死服務後,重啟服務並且調用onStartCommand(),但是不重新投遞上一次的intent。系統會傳給onStartCommand()函數null,除非有pending intent來啟動服務,會傳給onStartCommand()相應的intent。這種做法比較適合媒體播放服務,不需要執行命令,但是獨立運行,常處於等待任務的服務。
START_REDELIVER_INTENT 在系統殺死服務後,重啟服務並且調用onStartCommand(),參數傳入上一次的intent,然後接下來是pending intent傳入onStartCommand()。這比較適合於正在執行一項工作,它不能被立刻恢復,例如下載文件。
Intent intent = new Intent(this, HelloService.class);startService()會立刻返回,Android系統會調用服務的onStartCommand()函數。如果這是服務沒有在運行,系統會首先調用onCreate()函數,然後會緊接著調用onStartCommand()函數。
startService(intent);
Notification notification = new Notification(R.drawable.icon, getText(R.string.ticker_text),
System.currentTimeMillis());
Intent notificationIntent = new Intent(this, ExampleActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, getText(R.string.notification_title),
getText(R.string.notification_message), pendingIntent);
startForeground(ONGOING_NOTIFICATION_ID, notification);
GPUImage 是iOS下一個開源的基於GPU的圖像處理庫,提供各種各樣的圖像處理濾鏡,並且支持照相機和攝像機的實時濾鏡。GPUImage for Android是它在
最近研究HyBrid的兩種方式:一、直接原生WebView1)初始化WebView: //啟動javascript webView = (WebView)
一.包引入dependencies { compile fileTree(dir: 'libs', include: ['*.jar'
1. 網頁源碼查看器網頁源碼查看器案例實現在EditText中輸入網址,點擊按鈕獲取,獲取到網頁源碼,顯示在TextView上。在IE浏覽器中,快捷鍵Shift+F12可