Android: 从对话框启动警报服务
Android: Starting Alarm Service from Dialog
我正在尝试从对话框启动 1 分钟闹钟。这是我的对话方法(带有 AlarmManager 和所有东西):
public void Dialog2(){
new AlertDialog.Builder(context)
.setTitle("Email!")
.setMessage("Would You Like to Set a 1 Minute Reminder?\nRemember to Email Your Professor!")
.setPositiveButton("Set Alarm!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() +
60 * 1000, alarmIntent);
}
})
.setNegativeButton("Cancel!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
})
.show();
}
这是我的 AlarmReceiver Class:
public class AlarmReceiver extends WakefulBroadcastReceiver{
@Override
public void onReceive(final Context context, Intent intent) {
Toast.makeText(context, "Don't Forget To Email Your Teacher!.",
Toast.LENGTH_LONG).show();
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(2000);
Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alarmUri == null) {
alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}
Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
ringtone.play();
}
}
在我的 AndroidManifest 中,我包括:
<service android:enabled="true" android:name=".ServiceClass" />
<receiver android:name=".AlarmReceiver" />
这是我的服务Class:
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class ServiceClass extends Service{
@Override
public void onCreate() {
super.onCreate();
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
}
}
请帮忙!提前致谢!!
如果您想从 AlarmManager 启动服务,您应该在 AlarmReceiver 的 onReceive 方法中启动服务 class
您的基于 Android Dev Guide 的代码正在运行,更准确地说,这个:
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
alarmMgr.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP,SystemClock.elapsedRealtime() + 10 * 1000, alarmIntent);
Alarm.Receiver.java:
public class AlarmReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
/* class1 = new MainActivity();
class1.message();*/
Toast.makeText(context, "Don't Forget To Email Your Teacher!.",
Toast.LENGTH_LONG).show();
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(1000);
Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alarmUri == null)
{
alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}
Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
ringtone.play();
}
}
我需要做 3 件事才能让它发挥作用(API 16 到 25):
在您的清单中应用之前添加权限:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.SET_ALARM" />
<uses-permission android:name="android.permission.VIBRATE" />
在应用程序中移动接收器:
<receiver android:name=".Action.AlarmReceiver"> //change according to your path
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
最后...重新启动 Android Studio。
是的,我不知道为什么,但我必须这样做才能使其适用于我的情况。
希望对您有所帮助!
我正在尝试从对话框启动 1 分钟闹钟。这是我的对话方法(带有 AlarmManager 和所有东西):
public void Dialog2(){
new AlertDialog.Builder(context)
.setTitle("Email!")
.setMessage("Would You Like to Set a 1 Minute Reminder?\nRemember to Email Your Professor!")
.setPositiveButton("Set Alarm!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() +
60 * 1000, alarmIntent);
}
})
.setNegativeButton("Cancel!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
})
.show();
}
这是我的 AlarmReceiver Class:
public class AlarmReceiver extends WakefulBroadcastReceiver{
@Override
public void onReceive(final Context context, Intent intent) {
Toast.makeText(context, "Don't Forget To Email Your Teacher!.",
Toast.LENGTH_LONG).show();
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(2000);
Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alarmUri == null) {
alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}
Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
ringtone.play();
}
}
在我的 AndroidManifest 中,我包括:
<service android:enabled="true" android:name=".ServiceClass" />
<receiver android:name=".AlarmReceiver" />
这是我的服务Class:
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class ServiceClass extends Service{
@Override
public void onCreate() {
super.onCreate();
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
}
}
请帮忙!提前致谢!!
如果您想从 AlarmManager 启动服务,您应该在 AlarmReceiver 的 onReceive 方法中启动服务 class
您的基于 Android Dev Guide 的代码正在运行,更准确地说,这个:
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
alarmMgr.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP,SystemClock.elapsedRealtime() + 10 * 1000, alarmIntent);
Alarm.Receiver.java:
public class AlarmReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
/* class1 = new MainActivity();
class1.message();*/
Toast.makeText(context, "Don't Forget To Email Your Teacher!.",
Toast.LENGTH_LONG).show();
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(1000);
Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alarmUri == null)
{
alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}
Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
ringtone.play();
}
}
我需要做 3 件事才能让它发挥作用(API 16 到 25):
在您的清单中应用之前添加权限:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.SET_ALARM" />
<uses-permission android:name="android.permission.VIBRATE" />
在应用程序中移动接收器:
<receiver android:name=".Action.AlarmReceiver"> //change according to your path
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
最后...重新启动 Android Studio。 是的,我不知道为什么,但我必须这样做才能使其适用于我的情况。
希望对您有所帮助!