【发布时间】:2011-06-11 13:09:26
【问题描述】:
我一直在学习《Pro Android 2》这本书。我正在研究一个包含两个类的服务示例:BackgroundService.java 和 MainActivity.java。 MainActivity 声称(错误地?)它启动了服务,如从下面的 Log.d 调用到 logcat 的输出所示:
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d(TAG, "starting service");
Button bindBtn = (Button)findViewById(R.id.bindBtn);
bindBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent backgroundService = new Intent(MainActivity.this, com.marie.mainactivity.BackgroundService.class);
startService(backgroundService);
}
});
Button unbindBtn = (Button)findViewById(R.id.unbindBtn);
unbindBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
stopService(new Intent(MainActivity.this, BackgroundService.class));
}
});
}
}
让我感到困惑的是 UI 提供了两个按钮:如上所示的 Bind 和 UnBind。但是根据documentation if onBind() 如下所示返回null,表示你不想允许绑定。但如上所示(绑定按钮)bindBtn.setOnClickListener(new OnClickListener() 的 onClick() 方法调用 startService(backgroundService) 给出此错误:“无法启动服务 Intent { cmp=com.marie.mainactivity/.BackgroundService }:未找到”
public class BackgroundService extends Service {
private NotificationManager notificationMgr;
@Override
public void onCreate() {
super.onCreate();
notificationMgr = NotificationManager)getSystemService(NOTIFICATION_SERVICE);
displayNotificationMessage("starting Background Service");
Thread thr = new Thread(null, new ServiceWorker(), "BackgroundService");
thr.start();
}
class ServiceWorker implements Runnable
{
public void run() {
// do background processing here...
//stop the service when done...
//BackgroundService.this.stopSelf();
}
}
@Override
public void onDestroy()
{
displayNotificationMessage("stopping Background Service");
super.onDestroy();
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void displayNotificationMessage(String message)
{
Notification notification = new Notification(R.drawable.note, message, System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);
notification.setLatestEventInfo(this, "Background Service", message, contentIntent);
notificationMgr.notify(R.id.app_notification_id, notification);
}
}
我不明白这个例子的意义。如果 onBind() 返回 null 有一个绑定按钮 (bindBtn) 有什么意义?我认为重点是展示如何启动 BackgroundService。但除非我遗漏了什么,否则它似乎不起作用。
我应该添加我已添加到我的 AndroidManifest.xml:
<service android:name=".BackgroundService"></service>
如下:
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<service android:name=".BackgroundService"></service>
</activity>
</application>
【问题讨论】:
-
您是否已将 .BackgroundService 添加到 manifest.xml 中?
-
@是的,是的,正如你在上面看到的那样。
-
@Ye 但我在错误的地方添加了它,您可以从下面的@CaspNZ 答案中看到。