2013-11-04 31 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();