使用sms关键字为android启动警报

时间:2012-11-27 14:31:59

标签: android alarm

先生,我真的不知道如何让我的代码工作。它只是无法启动警报。它只是启动指定的活动并跳过报警部分。我究竟做错了什么。感谢您的帮助

public class EAlarmReceiver extends BroadcastReceiver {

public static String sender;
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Bundle bundle = intent.getExtras(); 
    Object[] pdusObj = (Object[]) bundle.get("pdus"); 
    SmsMessage[] messages = new SmsMessage[pdusObj.length]; 
    for (int i = 0; i<pdusObj.length; i++) 
    { 
            messages[i] = SmsMessage.createFromPdu ((byte[]) 
            pdusObj[i]); 
            sender = messages[i].getOriginatingAddress();
    } 

    for (SmsMessage msg : messages) {
        if (msg.getMessageBody().contains("firealert")) {

            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.SECOND, 0);

            Intent i = new Intent(context, ReceiverInterface.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context,
                12345, i, PendingIntent.FLAG_CANCEL_CURRENT);
            AlarmManager am = 
                (AlarmManager)context.getSystemService(Activity.ALARM_SERVICE);
            am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                    pendingIntent);
        }//end if
    }//end for

}// end onreceive

1 个答案:

答案 0 :(得分:1)

活动开始时应该为您提供正在发生的事情的线索。除非触发警报,否则代码中没有任何内容可以启动活动。警报实际上是被触发的,但是当你指定了当前时间(cal.add(Calendar.SECOND,0))时,它会立即触发,启动相应的PendingIntent。将所需时间添加到日历中,以便稍后触发警报。