2

我有以下代码来设置警报

  @Override
        public void onCreate(Bundle savedInstance) {
            super.onCreate(savedInstance);

            int [] progressVals = getPackets();

            displayList(progressVals, values);

            setAlarm();
        }

private void setAlarm(){
    DatabaseHandler db = new DatabaseHandler(this);
    Queue top = db.getTopQueue();

    if(top != null){
        String p = top.getPacket();
        double timestamp = top.getTimestamp();
        int qNum = top.getExNum();

        Intent intent = new Intent("com.dejalearn.algorithm.AlarmReciever.class");
        PendingIntent pendingIntent = PendingIntent.getBroadcast(DisplayAllActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.SECOND, 3);

        alarmManager.set(AlarmManager.RTC_WAKEUP, instance.getTimeInMillis(), pendingIntent);
        Toast.makeText(DisplayAllActivity.this, "Start Alarm", Toast.LENGTH_LONG).show();
        Log.i("DISPLAY ALL", "ALARM SET UP");
    }
}

由此,我尝试从实际运行的活动中运行的活动称为 AlarmReciever,它扩展了广播接收器,但是我的警报接收器中的 toast 永远不会运行。

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

    public class AlarmReciever extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i("FOUND", "THE MESSAGE");
            try{
                Toast.makeText(context, "made it", Toast.LENGTH_LONG).show();
            }catch(Exception e){
                Toast.makeText(context, "error", Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }

        }

    }

为什么我的新吐司从来没有运行过。我查看了多个示例,但似乎没有任何效果。我不确定出了什么问题。

4

2 回答 2

3

将您的意图更改为有效的意图:

Intent intent = new Intent(DisplayAllActivity.this, AlarmReciever.class);

确保将接收器添加到AndroidManifest.xml这样的:

<receiver name=".AlarmReciever" />
于 2013-09-13T03:50:15.960 回答
1

下面是一个对我有用的例子Alarm application

myAlarmActivity.class

long startTimeInMilli=setTimeInMilli-System.currentTimeMillis();

Log.d("MYAPP", "set time in milli"+setTimeInMilli);
Log.d("MYAPP", "Total time in milli:"+startTimeInMilli);

Intent intent = new Intent();
intent.setClass(AlarmMgrActivity.this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(AlarmMgrActivity.this, 234324243/*some random id*/, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
                        + startTimeInMilli, pendingIntent);
Toast.makeText(getBaseContext(), "Alarm set in " + startTimeInMilli/1000+ " seconds", Toast.LENGTH_SHORT).show();

下面是 myBroadcastReceiver 的代码:

public class MyBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        // TODO Auto-generated method stub
        //Toast.makeText(arg0, "Don't panik but your time is up!!!!.",
          //      Toast.LENGTH_LONG).show();

        // Vibrate the mobile phone
        /*Vibrator vibrator = (Vibrator) arg0.getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(10000);*/


         AudioManager am= (AudioManager)arg0.getSystemService(Context.AUDIO_SERVICE);

         if(AlarmMgrActivity.ToMode.equalsIgnoreCase("normal"))
             am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
         else if(AlarmMgrActivity.ToMode.equalsIgnoreCase("silent"))
             am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
         else if(AlarmMgrActivity.ToMode.equalsIgnoreCase("vibrate"))
             am.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);


         Intent i=new Intent();
         i.setClass(arg0, MyService.class);
         arg0.startService(i);
    }

}

在清单中,我在 Manifest.xml 中注册了我的广播接收器,如下所示:

<application>
.....
.....
<receiver android:name="MyBroadcastReceiver" /> 
.....
.....
</application>
于 2013-09-13T04:09:35.140 回答