2012-10-15 2 views
0

내 응용 프로그램에 지속적인 알림을 표시하고 싶습니다.2 분 후에 계속 알림 android

현재 4 초 후에 자동으로 알림을 표시 할 수 있지만 원하는 것은 특정 시간 간격 후에 계속해서 올리는 것입니다.

다음은 제 코드입니다.

내 xml 파일이

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
> 
<Button android:id="@+id/notify" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:text="Raise a notification" 
    android:onClick="notifyMe" 
    /> 
    <Button android:id="@+id/cancel" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:text="Clear the notification" 
    android:onClick="clearNotification" 
    /> 
</LinearLayout> 

이 내 활동 파일입니다

package com.vimaltuts.android.notificationexample; 

import java.util.Timer; 
import java.util.TimerTask; 
import android.app.Activity; 
import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 

     public class NotificationExampleActivity extends Activity { 

    private static final int NOTIFY_ME_ID=1987; 
    private int count=0; 
    private NotificationManager mgr=null; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    mgr=(NotificationManager)getSystemService(NOTIFICATION_SERVICE); 

    Timer timer = new Timer(); 
    timer.schedule(new TimerTask() 
    { 
     public void run() 
     { 
      View v = new View(getApplicationContext()); 
      notifyMe(v); 
     }},4000); 
    } 

    @SuppressWarnings("deprecation") 
    public void notifyMe(View v) 
    { 
    @SuppressWarnings("deprecation") 
    Notification note=new Notification(R.drawable.stat_notify_chat,"Status message!",System.currentTimeMillis()); 
    PendingIntent i=PendingIntent.getActivity(this,0,new Intent(this, NotificationMessage.class),0); 
    note.setLatestEventInfo(this, "New Email","Unread Conversation", i); 
    note.number=++count; 
    note.vibrate=new long[] {500L, 200L, 200L, 500L}; 
    note.flags|=Notification.FLAG_AUTO_CANCEL;  
    mgr.notify(NOTIFY_ME_ID, note); 
    } 

    public void clearNotification(View v) { 
    mgr.cancel(NOTIFY_ME_ID); 
    } 
} 

내가 추가 코드 (있는 경우)가 필요합니다 알려주세요?

서비스로도 가능합니다.


내가 코드 원수 테스트 목적으로 다음과 같은 시도하지만 내가 다음

잘못 어디로가는 일이 ... 사람이 말해 줄 수 did't 나의 활동 코드

Intent i= new Intent(NotificationExampleActivity.this,NotificationService.class); 
    PendingIntent pi=PendingIntent.getService(NotificationExampleActivity.this, 0, i, 0); 
    AlarmManager alarmMgr=(AlarmManager)getSystemService(ALARM_SERVICE); 
    alarmMgr.cancel(pi); 
    alarmMgr.setRepeating(alarmMgr.ELAPSED_REALTIME_WAKEUP, System.currentTimeMillis(),(10*1000), pi); 
    Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show(); 

하고 다음과 같다 내 방송 수신자

Toast.makeText(context, "Repeating Alarm worked.", Toast.LENGTH_LONG).show(); 

표시하고 싶습니다. msg = "반복 알람" 테벨

답변

1

알림과 함께 AlarmManager을 사용해보세요.

Activity에 반복 알람을 설정할 수 있습니다. 그런 다음 BroadcastReceiver을 확장하는 클래스를 만듭니다. onReceive 메소드를 대체하여 통지 코드를 작성할 수 있습니다.

이 튜토리얼은 here을 통해 매우 잘 설명됩니다.

관련 문제