启动警报对话框有时会工作,有时则不会

时间:2015-01-11 07:09:21

标签: android android-alertdialog

我正在使用以下代码启动警报对话框。这有时候效果很好。有时它没有。

`Intent alarmIntent = new Intent(“android.intent.action.MAIN”);

      alarmIntent.setClass(ct, AlertsDlgactivity.class);
      alarmIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        // Pass on the alarm ID as extra data
        // Start the popup activity
      ct.startActivity(alarmIntent);`

此外,很少会出现多个对话框实例,有关如何处理此

的任何帮助

1 个答案:

答案 0 :(得分:0)

在没有看到所有代码的情况下,我不确定您是如何实现此目的的,所以我提供了一个简单的函数来使用Alarm Manager:

public void scheduleAlarm(int year,int month,int day,int hr,int min,int sec, String message, int _id)
    {
        long future = new GregorianCalendar(year,month,day,hr,min,sec).getTimeInMillis();
        Intent intentAlarm = new Intent(this, AlarmReciever.class);
        intentAlarm.putExtra("_MESSAGE",message);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, future,
                PendingIntent.getBroadcast(this, _id, intentAlarm, PendingIntent.FLAG_ONE_SHOT));
        Toast.makeText(this, "Alarm Scheduled", Toast.LENGTH_LONG).show();
    }

这将是AlarmReciever.class

public class AlarmReciever  extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        //Use this to get variables passed from Main Activity  
        String message = intent.getStringExtra("_MESSAGE");
        
        //Do some stuff here
    }
}

另外不要忘记将这些添加到您的清单中:

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

<application
             <receiver android:name=".AlarmReciever"/>

</application>