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