2012-03-04 16 views
1

サービスクラスを拡張するBackgroundServiceクラスを作成しました。Androidステータス通知

私はチェックの間の継続時間を記録するタイマーを持っています。 onCreateメソッドでは10秒間設定していますので、10秒ごとにステータス通知メッセージを送信することが予想されます。私が抱えている問題は、10秒ごとに、私はそれを有効にした状態通知を「サウンド」と聞いていますが、最初の通知サービスアラートにのみ表示される画面上部にテキストリマインダーは表示されません。誰もがこれを修正する方法を知っていますか?

このクラスのソースは、次のとおりです。ありがとうございました!

プライベートタイマータイマー;

private TimerTask updateTask = new TimerTask() { 
    @Override 
    public void run() { 
     Log.i(TAG, "Timer task doing work!"); 

     // Process junk here: 
     sendNotification("Please leave in 5 min!!!"); 
    } 
}; 

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

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 

    return super.onStartCommand(intent, flags, startId); 
} 

@Override 
public void onCreate() { 
    // TODO Auto-generated method stub 
    super.onCreate(); 
    Log.i(TAG, "Background Service creating"); 

    timer = new Timer("DurationTimer"); 
    timer.schedule(updateTask, 1000L, 10*1000L); 
} 

public void sendNotification(CharSequence message) 
{ 
    // Execute Check and Notify 
    String ns = Context.NOTIFICATION_SERVICE; 
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); 
    int icon = R.drawable.simon; 
    CharSequence tickerText = message; 
    long when = System.currentTimeMillis(); 
    Notification notification = new Notification(icon, tickerText, when); 
    Context context = getApplicationContext(); 
    CharSequence contentTitle = "LEAVE NOW!"; 
    CharSequence contentText = "LEAVE NOW!!"; 
    Intent notificationIntent = new Intent(this, BackgroundService.class); 
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 
    notification.defaults |= Notification.DEFAULT_SOUND; 
    //notification.defaults |= Notification.DEFAULT_VIBRATE; 

    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 
    int HELLO_ID = 1; 
    mNotificationManager.notify(HELLO_ID, notification); 
} 
+0

caanあなたが受け入れられたコードをどこに追加したか教えてください。 – user3233280

答えて

0

各繰り返しでcontentTextの値を変更します。

例:

CharSequence contentText = "LEAVE NOW!! " + System.currentTimeMillis(); 

私はあなたがすでに送信したい通知と同じIDを持つ通知を持っているため、メッセージのテキストは常に変化していることがわかりますね。だから、テキストは単に変更されるだけです。

別の方法:あなたが新しい通知を作成する前に

mNotificationManager.clear(HELLO_ID); 

はそれを行います。

+0

System.currentTimeMillis()を別途使用してcontentText、contentTitle、HELLO_IDの値を変更しようとしましたが、2回目に表示されない通知テキストの問題は解決しませんでした。あなたはこれを引き起こすかもしれない何かを知っていますか?ありがとう。 – Garrett

+0

本文に新しいセクションが追加されました。 – Knossos

+0

mNotificationManager.cancel(HELLO_ID)を使用しました。ありがとう! – Garrett