public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void scheduleAlarm(View V){
//Long time= new GregorianCalendar().getTimeInMillis()+24*60*60*1000;
Long time= new GregorianCalendar().getTimeInMillis()+30*1000;
Intent intentAlarm= new Intent(this, AlarmReceiver.class);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, time, PendingIntent.getBroadcast(this, 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
Toast.makeText(this, "Alarm Scheduled for 30 repetd", Toast.LENGTH_LONG).show();
}
}
AlarmReceiverClass
public class AlarmReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "Alarm Triggered and SMS Sent", Toast.LENGTH_LONG);
}
}
这是我的代码我试图设置警报并在每30秒显示警报消息但是我无法在吐司中显示警报混乱,而我写代码所有的事情都正确但我不知道在哪里做错了请看看我的代码,请告诉我哪里出错了。
答案 0 :(得分:1)
首先请在吐司中添加.show()
。
即Toast.makeText(context, "Alarm Triggered and SMS Sent", Toast.LENGTH_LONG).show();
编辑
请使用以下代码:
清单中的
<receiver android:name=".alarm.manager.Alarm" >
</receiver>
创建一个名为AlarmService.java
public class AlarmService extends Service {
Alarm alarm = new Alarm();
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
alarm.SetAlarm(AlarmService.this);
return START_STICKY;
}
public void onStart(Context context, Intent intent, int startId) {
alarm.SetAlarm(context);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
另一类alarm.java
package alarm.manager;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.PowerManager;
import android.util.Log;
public class Alarm extends BroadcastReceiver {
public static final String ALARM_SERVICE = "alarm";
public Alarm() {
}
Context mContext;
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@SuppressLint("NewApi")
@Override
public void onReceive(Context context, Intent intent) {
PowerManager pm = (PowerManager) context
.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK, "");
wl.acquire();
mContext = context;
Log.d("App", "Alarm manager invoked");
wl.release();
}
public void SetAlarm(Context context) {
AlarmManager am = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, Alarm.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
1000, pi);
}
/**
* Stop alarm
*
* @param context
* = context of activity
*/
public void CancelAlarm(Context context) {
Intent intent = new Intent(context, Alarm.class);
PendingIntent sender = PendingIntent
.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
}
}
你班上的叫这些行
Alarm alarm = new Alarm();
alarm.SetAlarm(getApplicationContext());
答案 1 :(得分:0)
您错过了为.show()
调用Toast
方法。所以它将是
Toast.makeText(context, "Alarm Triggered and SMS Sent", Toast.LENGTH_LONG).show();