我希望我的應用程序每天早上6點以「Good Morning」消息顯示通知。正如我讀到的,爲此我需要應用程序在後臺運行,所以我需要使用服務。服務在指定的時間每天創建一個通知
我試過下面的代碼,但我卡住了。
MainActivity.java
public void onClickStartService(View v)
{
startService(new Intent(this,MyService.class));
}
public void onClickStopService(View v)
{
stopService(new Intent(this,MyService.class));
}
和MyService.java是
public class MyService extends Service{
private static final String TAG = "MyService";
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
Toast.makeText(this, "Congrats! MyService Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
}
@Override
public void onStart(Intent intent, int startId) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
//Note: You can start a new thread and use it for long background processing from here.
}
@Override
public void onDestroy() {
Toast.makeText(this, "MyService Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
}
}
我有按鈕來啓動和停止服務和它的作品。現在我想讓該服務創建通知,正如我在文章開頭提到的那樣。我怎樣才能做到這一點?