这里有警报代码
AlarmManagerExample.java
package com.example.alarmmanagerexample;
import java.util.Calendar;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
public class AlarmManagerExample extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm_manager_example);
try {
Button btnSet = (Button) findViewById(R.id.selectButton);
final DatePicker dp = (DatePicker) findViewById(R.id.scheduleTimePicker);
final TimePicker tp = (TimePicker) findViewById(R.id.timePicker1);
btnSet.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int day = dp.getDayOfMonth();
int month = dp.getMonth();
int year = dp.getYear();
int currentTime =tp.getCurrentHour();
int currentMinute=tp.getCurrentMinute();
// int hour =timePicker.getti
// Create a new calendar set to the date chosen
// we set the time to midnight (i.e. the first minute of that day)
Calendar c = Calendar.getInstance();
c.set(year, month, day);
c.set(currentTime, currentMinute);
String strDateTime = dp.getYear() + "-" + (dp.getMonth() + 1) + "-" + dp.getDayOfMonth() + " "+ tp.getCurrentHour() + ":" + tp.getCurrentMinute();
//Create a new PendingIntent and add it to the AlarmManager
Intent intent = new Intent(AlarmManagerExample.this, RingAlarm.class);
PendingIntent pendingIntent = PendingIntent.getActivity(AlarmManagerExample.this,c, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am =
(AlarmManager)getSystemService(Activity.ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(),
2*60*60,pendingIntent);
Toast.makeText(AlarmManagerExample.this, "User has selected " + strDateTime, Toast.LENGTH_LONG).show();
finish();// move to previous activity
}});
} catch (Exception e) {}
}
}
上面的代码在pendingintent行错误中出现错误
RingAlarm.java
package com.example.alarmmanagerexample;
import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
public class RingAlarm extends Activity {
MediaPlayer mp=null ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.alarm);
Button stopAlarm = (Button) findViewById(R.id.stopAlarm);
mp = MediaPlayer.create(getBaseContext(),R.raw.audio);
stopAlarm.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
mp.stop();
finish();
return false;
}
});
playSound(this, getAlarmUri());
}
private void playSound(final Context context, Uri alert) {
Thread background = new Thread(new Runnable() {
public void run() {
try {
mp.start();
} catch (Throwable t) {
Log.i("Animation", "Thread exception "+t);
}
}
});
background.start();
}
@Override
protected void onDestroy() {
super.onDestroy();
mp.stop();
} //Get an alarm sound. Try for an alarm. If none set, try notification,
//Otherwise, ringtone.
private Uri getAlarmUri() {
Uri alert = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alert == null) {
alert = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if (alert == null) {
alert = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
}
}
return alert;
}
}
Bellow给定的数据是布局页面
activity_alarm_manager_example.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Notificatin Create" />
<DatePicker
android:id="@+id/scheduleTimePicker"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TimePicker
android:id="@+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/selectButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onDateSelectedButtonClick"
android:text="Set notification for this date" />
</LinearLayout>
alarm.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:text="Stop Alarm" android:id="@+id/stopAlarm"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.alarmmanagerexample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.alarmmanagerexample.AlarmManagerExample"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".RingAlarm"
android:label="@string/app_name" />
</application>
</manifest>
如何解决这个问题??
答案 0 :(得分:0)
不要将Intent.FLAG_CANCEL_CURRENT
用于PendingIntent.getActivity,而是使用FLAG_ONE_SHOT
答案 1 :(得分:0)
我认为编译器抱怨这个:
PendingIntent pendingIntent = PendingIntent.getActivity(AlarmManagerExample.this,c, intent, PendingIntent.FLAG_CANCEL_CURRENT);
因为第二个参数c
需要是int
值。您似乎未在此处传递int
值。这是requestCode
参数。