gpt4 book ai didi

Android 闹钟无法使用 AlarmManager

转载 作者:行者123 更新时间:2023-11-29 01:48:02 27 4
gpt4 key购买 nike

我有一个我一直在做的安卓项目。我写了另一个android项目,我写的是单独测试警报。我将警报项目添加到我的主要项目,但是现在这不起作用,我根本没有收到任何警报。

工作报警项目代码为:警报演示.java 包 com.example.servicealarmdemo2;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.WindowManager.LayoutParams;
import android.widget.EditText;

public class AlertDemo extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
/** Turn Screen On and Unlock the keypad when this alert dialog is displayed */
getActivity().getWindow().addFlags(LayoutParams.FLAG_TURN_SCREEN_ON | LayoutParams.FLAG_DISMISS_KEYGUARD);
/** Creating a alert dialog builder */
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
/** Setting title for the alert dialog */
builder.setTitle("Alarm");
/** Setting the content for the alert dialog */
builder.setMessage("An Alarm by AlarmManager");
/** Defining an OK button event listener */
builder.setPositiveButton("Take", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
/** Exit application on click OK */
getActivity().finish();
}
});
builder.setNegativeButton("Snooze", new DialogInterface.OnClickListener() {
// @Override
public void onClick(DialogInterface dialog, int which) {
EditText text=MainActivity.text;
Intent i = new Intent("com.example.servicealarmdemo2.demoactivity");
PendingIntent operation = PendingIntent.getActivity(getActivity().getBaseContext(), 0, i, Intent.FLAG_ACTIVITY_NEW_TASK);
AlarmManager alarmManager = (AlarmManager)getActivity().getSystemService(Context.ALARM_SERVICE);
long time= System.currentTimeMillis();
String str = text.getText().toString();
long t=Long.parseLong(str);
alarmManager.set(AlarmManager.RTC_WAKEUP,
time+( t*60*1000), operation);
}
});
/** Creating the alert dialog window */
return builder.create();
}
/** The application should be exit, if the user presses the back button */
@Override
public void onDestroy() {
super.onDestroy();
getActivity().finish();
}
}

DemoActivity.java 包 com.example.servicealarmdemo2;

import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.v4.app.FragmentActivity;

public class DemoActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/** Creating an Alert Dialog Window */
AlertDemo alert = new AlertDemo();
/** Opening the Alert Dialog Window */
alert.show(getSupportFragmentManager(), "AlertDemo");
Vibrator mVibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
mVibrator.vibrate(300);
MediaPlayer mp = MediaPlayer.create(DemoActivity.this, R.raw.beep);
mp.start();
}
}

和 MainActivity.java: 包 com.example.servicealarmdemo2;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;

public class MainActivity extends Activity {
public static EditText text;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OnClickListener setClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
CheckBox box1=(CheckBox)findViewById(R.id.checkBox1);
CheckBox box2=(CheckBox)findViewById(R.id.checkBox2);
CheckBox box3=(CheckBox)findViewById(R.id.checkBox3);
CheckBox box4=(CheckBox)findViewById(R.id.checkBox4);
CheckBox box5=(CheckBox)findViewById(R.id.checkBox5);
CheckBox box6=(CheckBox)findViewById(R.id.checkBox6);
CheckBox box7=(CheckBox)findViewById(R.id.checkBox7);

/** Getting a reference to TimePicker object available in the MainActivity */
TimePicker tpTime = (TimePicker) findViewById(R.id.tp_time);
int hour = tpTime.getCurrentHour();
int minute = tpTime.getCurrentMinute();

Calendar c=Calendar.getInstance();
int month;
int day;
int year;

Date date =new Date();
c.setTime(date);

if(box1.isChecked()) {
/** This intent invokes the activity DemoActivity, which in turn opens the AlertDialog window */
Intent i = new Intent("com.example.servicealarmdemo2.demoactivity");
/** Creating a Pending Intent */
PendingIntent operation = PendingIntent.getActivity(getBaseContext(), 0, i, Intent.FLAG_ACTIVITY_NEW_TASK);
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);


month=c.get(Calendar.MONTH);
day=c.get(Calendar.DATE);
year=c.get(Calendar.YEAR);

/** Getting a reference to the System Service ALARM_SERVICE */
AlarmManager alarmManager1 = (AlarmManager) getBaseContext().getSystemService(ALARM_SERVICE);
/** Creating a calendar object corresponding to the date and time set by the user */
GregorianCalendar calendar = new GregorianCalendar(year,month,day, hour, minute);

/** Converting the date and time in to milliseconds elapsed since epoch */
long alarm_time = calendar.getTimeInMillis();

/** Setting an alarm, which invokes the operation at alart_time */
alarmManager1.set(AlarmManager.RTC_WAKEUP , alarm_time , operation);
alarmManager1.setRepeating(AlarmManager.RTC_WAKEUP,alarm_time,7*24*3600*1000, operation);
}

if(box2.isChecked()) {
Intent i = new Intent("com.example.servicealarmdemo2.demoactivity");
PendingIntent operation = PendingIntent.getActivity(getBaseContext(), 0, i, Intent.FLAG_ACTIVITY_NEW_TASK);
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK,Calendar.TUESDAY);

month=c.get(Calendar.MONTH);
day=c.get(Calendar.DATE);
year=c.get(Calendar.YEAR);
AlarmManager alarmManager2 = (AlarmManager) getBaseContext().getSystemService(ALARM_SERVICE);
GregorianCalendar calendar = new GregorianCalendar(year,month,day, hour, minute);
long alarm_time = calendar.getTimeInMillis();
alarmManager2.set(AlarmManager.RTC_WAKEUP , alarm_time , operation);
alarmManager2.setRepeating(AlarmManager.RTC_WAKEUP,alarm_time,7*24*3600*1000, operation);

}

if(box3.isChecked()) {
Intent i = new Intent("com.example.servicealarmdemo2.demoactivity");
PendingIntent operation = PendingIntent.getActivity(getBaseContext(), 0, i, Intent.FLAG_ACTIVITY_NEW_TASK);
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK,Calendar.WEDNESDAY);

month=c.get(Calendar.MONTH);
day=c.get(Calendar.DATE);
year=c.get(Calendar.YEAR);
AlarmManager alarmManager3 = (AlarmManager) getBaseContext().getSystemService(ALARM_SERVICE);
GregorianCalendar calendar = new GregorianCalendar(year,month,day, hour, minute);
long alarm_time = calendar.getTimeInMillis();
alarmManager3.set(AlarmManager.RTC_WAKEUP , alarm_time , operation);
alarmManager3.setRepeating(AlarmManager.RTC_WAKEUP,alarm_time,7*24*3600*1000, operation);
}

if(box4.isChecked()) {
Intent i = new Intent("com.example.servicealarmdemo2.demoactivity");
PendingIntent operation = PendingIntent.getActivity(getBaseContext(), 0, i, Intent.FLAG_ACTIVITY_NEW_TASK);
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK,Calendar.THURSDAY);

month=c.get(Calendar.MONTH);
day=c.get(Calendar.DATE);
year=c.get(Calendar.YEAR);
AlarmManager alarmManager4 = (AlarmManager) getBaseContext().getSystemService(ALARM_SERVICE);
GregorianCalendar calendar = new GregorianCalendar(year,month,day, hour, minute);
long alarm_time = calendar.getTimeInMillis();
alarmManager4.set(AlarmManager.RTC_WAKEUP , alarm_time , operation);
alarmManager4.setRepeating(AlarmManager.RTC_WAKEUP,alarm_time,7*24*3600*1000, operation);
}

if(box5.isChecked()) {
Intent i = new Intent("com.example.servicealarmdemo2.demoactivity");
PendingIntent operation = PendingIntent.getActivity(getBaseContext(), 0, i, Intent.FLAG_ACTIVITY_NEW_TASK);
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK,Calendar.FRIDAY);

month=c.get(Calendar.MONTH);
day=c.get(Calendar.DATE);
year=c.get(Calendar.YEAR);
AlarmManager alarmManager5 = (AlarmManager) getBaseContext().getSystemService(ALARM_SERVICE);
GregorianCalendar calendar = new GregorianCalendar(year,month,day, hour, minute);
long alarm_time = calendar.getTimeInMillis();
alarmManager5.set(AlarmManager.RTC_WAKEUP , alarm_time , operation);
alarmManager5.setRepeating(AlarmManager.RTC_WAKEUP,alarm_time,7*24*3600*1000, operation);
}

if(box6.isChecked()) {
Intent i = new Intent("com.example.servicealarmdemo2.demoactivity");
PendingIntent operation = PendingIntent.getActivity(getBaseContext(), 0, i, Intent.FLAG_ACTIVITY_NEW_TASK);
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK,Calendar.SATURDAY);

month=c.get(Calendar.MONTH);
day=c.get(Calendar.DATE);
year=c.get(Calendar.YEAR);
AlarmManager alarmManager6 = (AlarmManager) getBaseContext().getSystemService(ALARM_SERVICE);
GregorianCalendar calendar = new GregorianCalendar(year,month,day, hour, minute);
long alarm_time = calendar.getTimeInMillis();

alarmManager6.set(AlarmManager.RTC_WAKEUP , alarm_time , operation);
alarmManager6.setRepeating(AlarmManager.RTC_WAKEUP,alarm_time,7*24*3600*1000, operation);
}

if(box7.isChecked()) {
Intent i = new Intent("com.example.servicealarmdemo2.demoactivity");
PendingIntent operation = PendingIntent.getActivity(getBaseContext(), 0, i, Intent.FLAG_ACTIVITY_NEW_TASK);
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);

month=c.get(Calendar.MONTH);
day=c.get(Calendar.DATE);
year=c.get(Calendar.YEAR);
AlarmManager alarmManager7 = (AlarmManager) getBaseContext().getSystemService(ALARM_SERVICE);
GregorianCalendar calendar = new GregorianCalendar(year,month,day, hour, minute);
long alarm_time = calendar.getTimeInMillis();
alarmManager7.set(AlarmManager.RTC_WAKEUP , alarm_time , operation);
alarmManager7.setRepeating(AlarmManager.RTC_WAKEUP,alarm_time,7*24*3600*1000, operation);

}


/** Alert is set successfully */
Toast.makeText(getBaseContext(), "Alarm is set successfully",Toast.LENGTH_SHORT).show();
text=(EditText)findViewById(R.id.editText1);


}
};

OnClickListener quitClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
};

Button btnSetAlarm = ( Button ) findViewById(R.id.btn_set_alarm);
btnSetAlarm.setOnClickListener(setClickListener);

Button btnQuitAlarm = ( Button ) findViewById(R.id.btn_quit_alarm);
btnQuitAlarm.setOnClickListener(quitClickListener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

现在我尝试将同一个项目与我的主项目一起使用,主要是警报功能。我的主要项目的代码。

警报.java: 包 com.example.appzheimerbeta4.widgets.alerts;

import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.WindowManager.LayoutParams;

public class Alert extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
getActivity().getWindow().addFlags(LayoutParams.FLAG_TURN_SCREEN_ON | LayoutParams.FLAG_DISMISS_KEYGUARD);

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Alarm");
builder.setMessage("An alarm by AlarmManager");
builder.setPositiveButton("Take", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//TODO decrement pills here
getActivity().finish();
}
});
builder.setNegativeButton("Snooze", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(AlertActivity.class.getPackage().getName() + ".alertactivity");
PendingIntent operation = PendingIntent.getActivity(getActivity().getBaseContext(), 0, i, Intent.FLAG_ACTIVITY_NEW_TASK);
AlarmManager am = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);

final long snoozeTime = 2; // 2 mins
long time = System.currentTimeMillis() + (snoozeTime * 60 * 1000);

am.set(AlarmManager.RTC_WAKEUP, time, operation);
}
});

return builder.create();
}

@Override
public void onDestroy() {
super.onDestroy();
getActivity().finish();
}
}

AlertAcitivty.java: 包 com.example.appzheimerbeta4.widgets.alerts;

import com.example.appzheimerbeta4.R;

import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.v4.app.FragmentActivity;

public class AlertActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Alert alert = new Alert();
alert.show(getSupportFragmentManager(), "Alert Demo");

Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vib.vibrate(300);

MediaPlayer mp = MediaPlayer.create(AlertActivity.this, R.raw.beep);
mp.start();
}
}

主要 Activity .java 包 com.example.appzheimerbeta4;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import com.example.appzheimerbeta4.widgets.alerts.AlertActivity;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
public static final String patientID = "Patient1";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button b = (Button) findViewById(R.id.caregiverBtn); // using the caregiverBtn as a test subject to test for alerts
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
int hour = 22;
int minute = 11;

Calendar c = Calendar.getInstance();
int month;
int day;
int year;

Date date = new Date();
c.setTime(date);

Intent i = new Intent(AlertActivity.class.getPackage().getName() + ".alertactivity");
PendingIntent operation = PendingIntent.getActivity(getBaseContext(), 0, i, Intent.FLAG_ACTIVITY_NEW_TASK);

c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);

month = c.get(Calendar.MONTH);
day = c.get(Calendar.DATE);
year = c.get(Calendar.YEAR);

AlarmManager am = (AlarmManager) getBaseContext().getSystemService(ALARM_SERVICE);
GregorianCalendar calendar = new GregorianCalendar(year, month, day, hour, minute);

long alarmTime = calendar.getTimeInMillis();

am.set(AlarmManager.RTC_WAKEUP, alarmTime, operation);

Toast.makeText(getBaseContext(), "Alarm set", Toast.LENGTH_SHORT).show();
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

如果有人能帮我解决这个问题,请帮忙。过去几天我一直在研究它,但找不到任何修复方法。

注意:如果我需要提供其他代码,请告诉我。

最佳答案

当您创建 Intent像这样:

Intent i = new Intent(AlertActivity.class.getPackage().getName() + ".alertactivity");

您正在使用 Intent采用 ACTION 字符串的构造函数。这将创建一个新的 Intent并在 Intent 中设置 ACTION 字段到这个字符串。这用于创建一个隐式 Intent并且只有在您配置了 <intent-filter> 后才会开始您的 Activity 在您的警报 Activity 的 list 中。您还没有发布您的 list ,但我猜您还没有这样做。

如果你想开始你的警报 Activity ,只需创建 Intent像这样:

Intent i = new Intent(MainActivity.this, AlertActivity.class);

这使用了 IntentIntent 中设置组件的构造函数明确地

关于Android 闹钟无法使用 AlarmManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20215294/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com