我想了解Android中的服務類。如何在Android中啓動服務
我創建了一個非常簡單的活動:
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;
}
}
這個活動應該調用下面的服務,但現在看來,這不是因爲沒有我的日誌或吐司消息是顯示:
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>
你在Manifest文件中註冊服務嗎? – Yahor10
我有我..我剛剛添加我的清單的應用程序主體 – erik
從創建方法中刪除視圖元素 – Yahor10