我正在尝试了解 Android 中的 Service 类。
我创建了一个非常简单的活动:
public class ServiceLauncher extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_service_launcher);
Intent intent = new Intent(this, NavOverrideService.class);
startService(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_service_launcher, menu);
return true;
}
}
此活动应该调用以下服务,但它似乎不是因为我的日志或 toast 消息都没有显示:
public class NavOverrideService extends Service{
/* (non-Javadoc)
* @see android.app.Service#onBind(android.content.Intent)
*/
@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.
Log.v("Nav Service: ","Created");
View disableStatusBar = new TableLayout(getApplicationContext());
//mView = new HUDView(getApplicationContext());
WindowManager.LayoutParams handleParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.FILL_PARENT,
50,
// This allows the view to be displayed over the status bar
// WindowManager.LayoutParams.TYPE_SYSTEM_ALERT |
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
// this is to keep button presses going to the background window
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH |
// this is to enable the notification to recieve touch events
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
// Draws over status bar
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
PixelFormat.TRANSPARENT);
handleParams.gravity = Gravity.TOP;
handleParams.y = 750;
//disableStatusBar.setBackgroundColor(Color.GREEN);
disableStatusBar.setBackgroundResource(R.drawable.fake_nav);
WindowManager winMgr = (WindowManager)getSystemService(WINDOW_SERVICE);
winMgr.addView(disableStatusBar, handleParams);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
Log.v("Nav Service: ","started");
// If we get killed, after returning from here, restart
return START_REDELIVER_INTENT;
}
@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();
}
}
我究竟做错了什么?
哦,我的清单:
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<service android:name="com.eliddell.services_test.NavOverrideService" />
<activity
android:name="com.eliddell.services_test.ServiceLauncher"
android:label="@string/title_activity_service_launcher" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>