android警报未触发

时间:2014-07-02 09:45:52

标签: android alarmmanager android-4.4-kitkat

我正在尝试制作一个简单的Android闹钟应用程序,它基本上可以从用户那里花时间并在那个时间每天设置闹钟

我正在使用闹钟手,应用程序运行良好,没有错误,但闹钟不起作用!!

这是用户设置时间的位置

buttonSet.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.i("syso","1");
            String sTime = eReminderTimeAM.getText().toString();
            String aTime[] = sTime.split(":");
            Log.i("syso","2");
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());               
            calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(aTime[0]));
            calendar.set(Calendar.MINUTE, Integer.parseInt(aTime[1]));

            Log.i("syso",aTime[1]);

            alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
            Intent intent = new Intent( getBaseContext(), Alarm.class);
            alarmIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
            Log.i("syso","3");

            alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                    AlarmManager.INTERVAL_DAY, alarmIntent);


        }
    }); 

这是另一个应该运行的活动

MediaPlayer mp=null ;

 @Override
 protected void onCreate(Bundle savedInstanceState) {

             super.onCreate(savedInstanceState);
             this.requestWindowFeature(Window.FEATURE_NO_TITLE);
             this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                     WindowManager.LayoutParams.FLAG_FULLSCREEN);

             setContentView(R.layout.activity_alarm);
             Button stopAlarm = (Button) findViewById(R.id.stopAlarm);

             mp = MediaPlayer.create(getBaseContext(),R.raw.toha);


             stopAlarm.setOnTouchListener(new OnTouchListener() {

                 @Override
                 public boolean onTouch(View arg0, MotionEvent arg1) {
                     // TODO Auto-generated method stub
                     mp.stop();
                     finish();
                     return false;
                 }
             });

             playSound(this, getAlarmUri());
         }

         private void playSound(final Context context, Uri alert) {


             Thread background = new Thread(new Runnable() {
                 public void run() {
                     try {

                        mp.start();

                     } catch (Throwable t) {
                         Log.i("Animation", "Thread  exception "+t);
                     }   
                 }
          });
          background.start();
        }

         @Override
         protected void onDestroy() {
             super.onDestroy();
             mp.stop();
         }      

3 个答案:

答案 0 :(得分:0)

“Alarm.class”是“BroadcastReceiver”吗?否则请创建一个“BroadcastReceiver”和onNeceive方法的“BroadcastReceiver”调用活动

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
    }
}

不要忘记将接收器添加到您的清单

答案 1 :(得分:0)

您必须添加接收类

public class Alarm extends BroadcastReceiver {
private Context context;
@Override
public void onReceive(Context context1, Intent intent) {

    context = context1;


   }
}
android清单中的

也声明了接收器

 <receiver android:name="Alarm" >

答案 2 :(得分:0)

最后这是一个愚蠢的错误..

只需将PendingIntent.getBroadcast替换为PendingIntent.getActivity,就可以了。