0
Android上的服務的很多示例和教程都適用於bound services,但如果我想創建未綁定的服務而不必處理綁定呢?如何在Android中創建未綁定的服務?
注意潛在downvoters
請downvoting之前爲什麼answering your own questions is a good thing讀了。
Android上的服務的很多示例和教程都適用於bound services,但如果我想創建未綁定的服務而不必處理綁定呢?如何在Android中創建未綁定的服務?
注意潛在downvoters
請downvoting之前爲什麼answering your own questions is a good thing讀了。
做的第一件事是將服務添加到您的清單中,<application>
標籤中:
<application ...>
...
<service
android:name=".RecordingService"
android:exported="false">
</application>
然後我們創建實際的服務類:
public class RecordingService extends Service {
private int NOTIFICATION = 1; // Unique identifier for our notification
public static boolean isRunning = false;
public static RecordingService instance = null;
private NotificationManager notificationManager = null;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate(){
instance = this;
isRunning = true;
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId){
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);
// Set the info for the views that show in the notification panel.
Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher) // the status icon
.setTicker("Service running...") // the status text
.setWhen(System.currentTimeMillis()) // the time stamp
.setContentTitle("My App") // the label of the entry
.setContentText("Service running...") // the content of the entry
.setContentIntent(contentIntent) // the intent to send when the entry is clicked
.setOngoing(true) // make persistent (disable swipe-away)
.build();
// Start service in foreground mode
startForeground(NOTIFICATION, notification);
return START_STICKY;
}
@Override
public void onDestroy(){
isRunning = false;
instance = null;
notificationManager.cancel(NOTIFICATION); // Remove notification
super.onDestroy();
}
public void doSomething(){
Toast.makeText(getApplicationContext(), "Doing stuff from service...", Toast.LENGTH_SHORT).show();
}
}
所有這些服務確實是在運行時顯示通知,並且在調用doSomething()
方法時可以顯示吐司。
正如你會注意到的,它被實現爲singleton,跟蹤它自己的實例 - 但沒有通常的靜態singleton工廠方法,因爲服務自然是單例並且是由意圖創建的。這個實例在運行時對於服務獲得「處理」是有用的。
最後,我們需要啓動和從活動停止服務:
public void startOrStopService(){
if(RecordingService.isRunning){
// Stop service
Intent intent = new Intent(this, RecordingService.class);
stopService(intent);
}
else {
// Start service
Intent intent = new Intent(this, RecordingService.class);
startService(intent);
}
}
在這個例子中,該服務已啓動,並用同樣的方法停止,這取決於它的當前狀態。
我們也可以從我們的活動調用doSomething()
方法:
public void makeServiceDoSomething(){
if(RecordingService.isRunning)
RecordingService.instance.doSomething();
}
你只要回答在一分鐘內你自己的帖子...!? – Shaishav
@Shaishav是的,我做到了。事實上,在我發佈之前,我使用「Ask a question」頁面底部的複選框回答了它。我找不到任何關於如何在Stackoverflow上創建一個簡單的未綁定服務的清晰完整的演示,所以我想我會發布我自己的,現在我知道如何去做。 [閱讀更多關於回答你自己的問題。](http://stackoverflow.com/help/self-answer) – BadCash
很酷。謝謝(你的)信息。 – Shaishav