2010-08-30 41 views
0

HI, 我寫一個簡單的服務應用,下面是活動和服務的代碼..,當我調用startService(),和stopService()其工作正常進行的一次,在我它必須給予通知..從下一次起如果再次調用startService()和stopService()它沒有給出想要的結果...問題服務

如果我缺少一些東西,請幫助我.... ThanQ 。

----------這就是我的活動類------------- 包com.mypack.serviceex;

進口android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button;

公共類serviceex延伸活動實現Button.OnClickListener { /**當首先創建活動調用。 */ Button bt1,bt2; public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main);

bt1 = (Button) findViewById(R.id.Button01); 
    bt2 = (Button) findViewById(R.id.Button02); 

    bt1.setOnClickListener(this); 
    bt2.setOnClickListener(this); 


} 
@Override 
public void onClick(View v) { 
    if(v == bt1) 
    { 
     Intent i = new Intent(serviceex.this,myservice.class); 
     Log.i("err","onClick(View v...."); 
     startService(i); 
    } 
    else if(v == bt2) 
    { 
     Intent i = new Intent(serviceex.this,myservice.class); 
     Log.i("err","else if(v == bt2)........"); 
     stopService(i); 
    } 

} 

}

 --------- this is my service ------------- 

包com.mypack.serviceex;

進口android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log;

公共類的MyService擴展服務 {

private NotificationManager nmgr; 
public void onCreate() 
{ 
    super.onCreate(); 
    nmgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    Log.i("err","onCreate.........."); 
    Thread th = new Thread(null,new incls(),"service..."); 

} 
public void onStart(Intent intent,int sid) 
{ 
    super.onStart(intent, sid); 
    Log.i("err","onStart........"); 
} 
public void onDestroy() 
{ 
    super.onDestroy(); 
    Log.i("err","onDestroy.........."); 
    displayMessage("Stopping Service"); 

} 
public void displayMessage(String str) 
{ 
    Log.i("err.","displayMessage....."); 
    Notification nf = new Notification(R.drawable.icon,str,System.currentTimeMillis()); 
    PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this,myservice.class), 0); 
    nf.setLatestEventInfo(this, "Service...", str, pi); 
    nmgr.notify(R.string.uid, nf); 


} 

private class incls implements Runnable 
{ 
    public void run() 
    { 
     Log.i("err","public void run().........."); 
     System.out.println("In Runnn"); 


    } 



} 
@Override 
public IBinder onBind(Intent arg0) { 
    // TODO Auto-generated method stub 
    return null; 
} 

}

+0

什麼API級別,你定位? – codinguser 2010-08-30 11:52:29

+0

我有趣的水平2.1 – Brigadier 2010-08-30 12:16:46

回答

0

你的方法都不是@Override荷蘭國際集團類的方法。你必須用@Override來表示它們。此外,在2.1上,您應該使用onStartCommand()而不是onStart()。另外請注意,撥打startService()多次只會撥打onCreate()一次

0

@Override是否絕對必要? Java規範沒有說你仍然可以重寫一個沒有@Override的方法,但是如果方法實際上並沒有重寫,那麼註釋會導致編譯錯誤?