2013-11-04 37 views
0

我將在我的應用程序中使用後臺服務,我正在使用一些代碼,但它沒有工作。如何在android中使用後臺服務

public class MyService extends Service { 


    String tag="TestService"; 
    @Override 
    public void onCreate() { 
    super.onCreate(); 
    Toast.makeText(this, "Service created...", Toast.LENGTH_LONG).show();  
    Log.i(tag, "Service created..."); 
    } 

    @Override 
    public void onStart(Intent intent, int startId) {  
    super.onStart(intent, startId); 
    Log.i(tag, "Service started..."); 
     } 
    @Override 
    public void onDestroy() { 
    super.onDestroy(); 
    Toast.makeText(this, "Service destroyed...", Toast.LENGTH_LONG).show(); 
    } 

    public IBinder onBind(Intent intent) { 
    return null; 
} 
} 
+1

什麼是不工作? – kabuko

回答

0

請注意onStart()已被棄用,使用onStartCommand()代替。

你如何開始你的服務?你應該從你的活動啓動它,使用:

startService(new Intent(this, MyService.class)); 

此外,在服務方面,使用:的

Toast.makeText(getApplicationContext(), "...", Toast.LENGTH_SHORT).show(); 

代替:

Toast.makeText(this, "...", Toast.LENGTH_SHORT).show();