0

当我单击我的一个片段内的按钮时,我试图设置一个测试警报。但是警报却没有出现。

public class AlarmReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub

            Toast.makeText(context, "OnReceive alarm test", Toast.LENGTH_SHORT).show();
        }
    }

//Button is inside a Fragment
Button SetAlarmButton = (Button)(v.findViewById(R.id.SetAlarmButton));       
        SetAlarmButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                try{
                    Calendar alarm = Calendar.getInstance();

                    alarm.set(Calendar.HOUR_OF_DAY, 18);
                    alarm.set(Calendar.MINUTE, 00);
                    long alarmMillis = alarm.getTimeInMillis();
                    Intent intent = new Intent(view.getContext(), AlarmReceiver.class);
                    PendingIntent pendingIntent = PendingIntent.getBroadcast(view.getContext(), 192837, intent, 
                                                              PendingIntent.FLAG_UPDATE_CURRENT);
                    AlarmManager am = (AlarmManager) view.getContext().getSystemService(view.getContext().ALARM_SERVICE);
                    am.set(AlarmManager.RTC_WAKEUP, alarmMillis , pendingIntent);

                }catch(Throwable e){AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
                    StringWriter sw = new StringWriter();
                    PrintWriter pw = new PrintWriter(sw);
                    e.printStackTrace(pw);
                    sw.toString();  
                    builder.setMessage(sw.toString());
                    AlertDialog alert = builder.create();
                    alert.show();
                }
        }}); 

我知道我需要修改我的清单并添加类似的东西,但我不知道如何以及在哪里

<receiver android:name="AlarmReceiver">
            ....
        </receiver>

我的清单

<?xml version="1.0" encoding="UTF-8"?>
<manifest android:versionCode="1" android:versionName="1.0"
    package="com.test.alarm" xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-sdk android:minSdkVersion="4" android:targetSdkVersion="15"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> 
    <application android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" android:theme="@style/AppTheme">
        <activity android:label="@string/title_activity_main" android:name=".Main">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity 
            android:name=".First_Start"
        />    
        <activity
            android:name=".Preferences"
            android:label="Settings">
        </activity>
    </application>
</manifest>
4

2 回答 2

0

查看 AlarmManager 的文档:http: //developer.android.com/reference/android/app/AlarmManager.html

您设置的闹钟与时钟应用程序内置的闹钟功能无关。

系统使用警报来允许应用程序在将来响应某些事件。您基本上告诉系统您希望您的应用程序在一段时间内被唤醒,并且您定义警报响起时调用的 Activity。

于 2012-10-07T16:40:40.767 回答
0
Intent i = new Intent(AlarmClock.ACTION_SET_ALARM);
i.putExtra(AlarmClock.EXTRA_MESSAGE, "New Alarm");
i.putExtra(AlarmClock.EXTRA_HOUR, hours);
i.putExtra(AlarmClock.EXTRA_MINUTES, mins);
startActivity(i);

但是有了这个我无法指定我想要设置闹钟的日期

于 2012-10-07T16:46:29.827 回答