【发布时间】:2015-03-15 19:17:16
【问题描述】:
我在android studio中做了一个闹钟。我可以运行该应用程序,除了播放闹钟铃声之外,一切正常。事实上,闹钟时间到来时没有任何声音。 我不知道我的代码有什么问题。请帮我找出错误。
主活动:
package com.mycompany.alarmclock;
//I haven't shown the imported stuff here. they are in my file.
import android.support.v7.app.ActionBarActivity;
public class MainActivity extends Activity {
AlarmManager alarmManager;
private PendingIntent pendingIntent;//an action to be performed by other/foreign application
private TimePicker alarmTimePicker;//In where the user set the time
private static MainActivity inst;
private TextView alarmTextView;//the area where alarm message/notification will be displayed
public static MainActivity instance() {
return inst;
}
public void onStart() {
super.onStart();
inst = this;
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alarmTimePicker = (TimePicker) findViewById(R.id.alarmTimePicker);
alarmTextView = (TextView) findViewById(R.id.alarmText);
ToggleButton alarmToggle = (ToggleButton) findViewById(R.id.alarmToggle);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
}
public void onToggleClicked(View view) {
if (((ToggleButton) view).isChecked()) {//if toggle button is "ON" do the alarming function
Log.d("MainActivity", "Alarm On");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute());
Intent myIntent = new Intent(MainActivity.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent, 0);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
} else {
alarmManager.cancel(pendingIntent);
setAlarmText("");
Log.d("MyActivity", "Alarm Off");
}
}
public void setAlarmText(String alarmText) {
alarmTextView.setText(alarmText);
}
}
报警接收器:
package com.mycompany.alarmclock;
//I haven't shown the imported stuff here. they are in my file.
import android.support.v4.content.WakefulBroadcastReceiver;
public class AlarmReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
MainActivity inst=MainActivity.instance();
inst.setAlarmText("Get Up! Get up!");
Uri alarmUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alarmUri == null) {
alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}
Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
ringtone.play();
//this will send a notification message
ComponentName comp = new ComponentName(context.getPackageName(),
AlarmService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
报警服务:
package com.mycompany.alarmclock;
//I haven't shown the imported stuff here. they are in my file.
import android.support.v4.app.NotificationCompat;
public class AlarmService extends IntentService {
private NotificationManager alarmnotificationManager;
public AlarmService(){
super("AlarmService");
}
@Override
protected void onHandleIntent(Intent intent) {
sendNotification("Get up! Get up!");
}
private void sendNotification(String msg){
Log.d("AlarmService","Sending notification...:"+msg);
alarmnotificationManager=(NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
NotificationCompat.Builder alamNotificationBuilder = new NotificationCompat.Builder(
this).setContentTitle("Alarm").setSmallIcon(R.drawable.ic_launcher)
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
alamNotificationBuilder.setContentIntent(contentIntent);
alarmnotificationManager.notify(1, alamNotificationBuilder.build());
Log.d("AlarmService", "Notification sent.");
}
}
【问题讨论】:
-
你卡在哪里了? (不要全部说出来)
-
我已经更新了我的帖子,增加了一些额外的解释。代码/应用程序运行良好。设置闹钟时间后,没有声音播放。没有警报通知发生 - 这是唯一的问题。
-
好的,所以警报根本不起作用,对吧?
-
不,它根本不起作用。
-
您是否忘记在
AlarmReceiver的清单中添加<receiver>标记?
标签: android android-intent android-activity alarmmanager android-pendingintent