我正在尝试制作一个简单的警报触发器。当警报响起时,应该有一个简单的声音警报。但是警报没有响起,什么也没有发生。现在我不确定我在哪里弄错了 - 是时代吗?意图?接收器?
我在其中使用 AlarmManager 的类
package com.example.prva;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
public class Meni_Splash extends Activity{
Calendar c = Calendar.getInstance();
private int hour = c.get(Calendar.HOUR_OF_DAY);
private int minute = c.get(Calendar.MINUTE);
int milivreme;
int milipicker;
long sveskupa;
int hoursad;
int minutesad;
protected boolean timeactive = true;
Handler h=new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
h.post(new Runnable(){
@Override
public void run() {
String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
TextView text = (TextView) findViewById(R.id.textDate);
text.setText(currentDateTimeString);
h.postDelayed(this, 1000);
}
});
final TimePicker picker = (TimePicker) findViewById(R.id.timePicker);
final TextView text = (TextView) findViewById(R.id.texttime);
picker.setIs24HourView(true);
picker.setCurrentHour(hour);
picker.setCurrentMinute(minute);
Button btnt = (Button) findViewById(R.id.buttontime);
btnt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
hoursad = picker.getCurrentHour();
minutesad = picker.getCurrentMinute();
milipicker = (((hoursad) * 60 * 60 * 1000) + ((minutesad) * 60 * 1000));
text.setText(new StringBuilder().append(hoursad).append(":").append(minutesad).append( "Trenutno vrijeme u milisekundama : ").append(System.currentTimeMillis()).append(" vrijeme izabrano u milisekundama sve skupa ").append(milipicker));
}
});
Button btnalarm = (Button) findViewById(R.id.btnalarm);
btnalarm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
hoursad = picker.getCurrentHour();
minutesad = picker.getCurrentMinute();
milivreme = (((hour)* 60 * 60 * 1000)+ ((minute) * 60 * 1000));
milipicker = (((hoursad) * 60 * 60 * 1000) + ((minutesad) * 60 * 1000));
sveskupa = milipicker - milivreme;
Intent intent = new Intent(Meni_Splash.this, AlarmReceiver.class);
PendingIntent pendingintent = PendingIntent.getActivity(Meni_Splash.this, 2, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + sveskupa, pendingintent);
}
});
Button btnv = (Button) findViewById(R.id.buttonv);
btnv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Meni_Splash.this, button.class));
}
});
}
}
警报接收器类:
package com.example.prva;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
public class AlarmReceiver extends Activity {
protected MediaPlayer MPAlarm;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
MPAlarm = MediaPlayer.create(this, R.raw.splash);
MPAlarm.start();
}
};