2014-11-22 7 views
1

알람 관리자를 사용하여 알림을 생성하려고합니다. 하지만 작동하지 않는 것 같습니다. 다른 ID를 사용하여 여러 알림을 설정하려고하지만 내 브로드 캐스트 수신기가 호출되지 않습니다. 나는 어떤 통보 또는 어떤 소리도 보지 않고있다.알람 미리 알림이 작동하지 않습니다.

나는 30-40 번 시도했다. 내가 여기에 25/11/2014

처럼, 캘린더 날짜를 설정하고

경보 알림을 설정 myactivity 코드입니다.

Calendar calendar = Calendar.getInstance(); 


    calendar.set(Calendar.YEAR, 2014); 
    calendar.set(Calendar.MONTH, 11); 
    calendar.set(Calendar.DAY_OF_MONTH, 25); 

    calendar.set(Calendar.HOUR_OF_DAY, 19); 
    calendar.set(Calendar.MINUTE, 30); 
    //calendar.set(Calendar.SECOND, 1); 

    Intent myIntent = new Intent(CreateReminder.this, MyReceiver.class); 
    myIntent.putExtra("reminder_id",reminderid); 
    myIntent 
    .setAction("com.sandeep.alarm.REMINDER"); 


    PendingIntent pendingIntent = PendingIntent.getBroadcast(
      CreateReminder.this, reminderid, myIntent, 0); 

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 
      pendingIntent); 

내가 권한을 AndroidManifest.xml에 내 수신기를 등록하고

public class MyReceiver extends BroadcastReceiver { 

private Ringtone r; 

@SuppressWarnings("deprecation") 
@Override 
public void onReceive(Context context, Intent intent) { 
    int ID=intent.getExtras().getInt("reminder_id"); 

    Log.i("CreateReminder", "reminder_id:-"+ID); 

    int icon = R.drawable.ic_launcher; 
     long when = System.currentTimeMillis(); 
     NotificationManager notificationManager = (NotificationManager) context 
     .getSystemService(Context.NOTIFICATION_SERVICE); 
     Notification notification = new Notification(icon, "reminder", when); 
     String title = context.getString(R.string.app_name); 
     String subTitle = "Please complete task"; 
     Intent notificationIntent = new Intent(context, ReminderActivity.class); 
     notificationIntent.putExtra("reminder_id", ID); 
     PendingIntent intent1 = PendingIntent.getActivity(context, 0,notificationIntent, 0); 
     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
     | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

     notification.setLatestEventInfo(context, title, subTitle, intent1); 

     //To play the default sound with your notification: 
     //notification.defaults |= Notification.DEFAULT_SOUND; 
     notification.flags |= Notification.FLAG_INSISTENT; 

     //notification.defaults |= Notification.; 
     notificationManager.notify(0, notification); 
     Uri notification1 = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); 
     r = RingtoneManager.getRingtone(context, notification1); 

     r.play(); 

     new Handler().postDelayed(new Runnable(){ 
      public void run() { 
       r.stop(); 
      } 
     }, 10000); 

    } 
}  

MyReceiver.class.

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" /> 
<uses-permission android:name="android.permission.VIBRATE" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="com.sandip.remindme.CreateReminder" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity android:name="com.sandip.remindme.ReminderActivity"></activity> 

    <receiver android:name="com.sandip.remindme.MyReceiver" 
     android:exported="false" > 
     <intent-filter> 
      <action android:name="com.sandeep.alarm.REMINDER" /> 
      </intent-filter> 
    </receiver> 
</application>  

나는 왜 작동하지 않는지 이해할 수 없습니다. 나에게 힌트 또는 참조를주세요.

+1

캘린더가 올바르게 설정되어 있습니까? 월은 0을 기준으로합니다. 즉, Calendar.JANUARY == 0. 또한 현재 시간으로 설정하려고한다면'calendar.set()'호출이 필요 없다. 'Calendar.getInstance()'는 현재 시간이 이미 설정된 인스턴스를 반환합니다. –

+0

난 분명히 모든 일정 것들을 버리고 (당신은 TimeZone 문제도있을 것이다) System.currentTimeMillis() + (30 * 1000)을 사용하고 30 초 후에 알람이 울리 리라 기대한다.그렇지 않으면 모든 구성 요소의 배선에 문제가 있습니다. 그렇다면 Calendar 논리가 실제로 작동하지 않습니다. –

+0

@Sandip 테스트를 위해 제거 된'calendar.set()'메소드를 사용하여 코드를 실행했고, 예상대로 바로 실행됩니다. 시간이 어떻게 든 잘못 설정되어 있지 않다면 내가 생각할 수있는 유일한 것은 수신자가 올바른 폴더에 있지 않다는 것입니다. –

답변

1

의도 한대로 동작을 설정 한 후에는 방송을 보내지 않습니다.

Intent myIntent = new Intent(CreateReminder.this, MyReceiver.class); 
    myIntent.putExtra("reminder_id",reminderid); 
    myIntent 
    .setAction("com.sandeep.alarm.REMINDER"); 
sendbroadcast(myIntent);//YOU FORGOT TO ADD THIS 
+1

Intent를 사용하여 AlarmManager에 전달할 PendingIntent를 생성합니다. 이 시점에서 방송 될 의도는 없습니다. –

+0

예 @MikeM! 문제는 잘못된 날짜를 미리 알림으로 설정하는 데있었습니다. 나는'January = 0' 대신에'January = 1'을 설정하고 있습니다. @ archon92 그것은 sendBroadcast (인 텐트)와 관련이 없습니다. –

0

브로드 캐스트를 보내지 않을 것입니다. 당신이 .setAction을 직접 후 코드가 잘 작동합니다

sendbroadcast(myIntent); 

추가.

+1

Intent는 AlarmManager에 전달 될 PendingIntent를 생성하는 데 사용됩니다. 이 시점에서 방송 될 의도는 없습니다. –

0

나는 또한 알람 잔여 작업 중이며 앱에서 성공적으로 구현했습니다. 다음 링크에서 예제 코드를 다운로드 할 수 있습니다. 나는 그것이 당신을 돕기를 바랍니다. 알림은 브로드 캐스트 리시버에 전화하는 데 30 초에서 1 분이 걸립니다. 즉 오후 3시 30 분에 알람을 설정하면 오후 3시 30 분경에 브로드 캐스트 수신자가 호출됩니다.

https://www.dropbox.com/s/t2m5ph8s8f17v6m/AndroidAlarmManager.zip?dl=0

감사

라 메쉬

0

나는

com.sandeep.alarm인가 ... 광산 코드를 비교하고 당신이 시도 할 몇 가지가있다 실제 네임 스페이스가 맞습니까? 앱 내의 활동에는 서로 다른 네임 스페이스 com.sandip.remindme이 있으므로 ("com.sandip.remindme.REMINDER")을 작업 이름으로 사용할 수 있습니다.

사용자의 의도는 특정 컨텍스트 및 클래스 대상으로 생성됩니다. 액션 이름만으로 구성하면 액션 이름이 문제인지 알 수 있습니다.

Intent myIntent = new Intent("com.sandip.remindme.REMINDER") 
관련 문제