2012-10-23 92 views
0

我想了解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> 
+0

你在Manifest文件中註冊服務嗎? – Yahor10

+0

我有我..我剛剛添加我的清單的應用程序主體 – erik

+0

從創建方法中刪除視圖元素 – Yahor10

回答

2

我創建一個新項目。 它適用於我:

public class MainActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     startService(new Intent(this, ServiceTest.class)); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 
} 


public class ServiceTest extends Service { 

    @Override 
    public void onCreate() { 
     Log.v(null, "SERVICE TEST onCreate"); 
     super.onCreate(); 
    } 

    @Override 
    public void onStart(Intent intent, int startId) { 
     Log.v(null, "SERVICE TEST onStart"); 
     super.onStart(intent, startId); 
    } 


    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 

} 


<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.servicetest" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="15" /> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/title_activity_main" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <service android:name="ServiceTest"></service> 
    </application> 

</manifest> 
+0

是因爲我有我的服務作爲外部類?我的主要活動是在包com.eliddell.service_test和我的服務在包com.eliddell.service_test.services? – erik

+0

即時獲取以下日誌:10-23 10:22:30.294:W/ActivityManager(199):無法啓動服務意圖{cmp = com.example.services_test/com.eliddell.services_test.services.NavOverrideService}:找不到 – erik

+0

謝謝...發現我的清單中有一個不好的軟件包名稱.. – erik