我有一个应用程序,其中警报需要对存储在数据库中的时间值进行播放。当我删除语句“notification.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;”时,会在该警报时间出现通知没有播放闹钟。当我插入上面的语句时,警报会通过强行压缩我的应用程序来播放。我的警报类如下:
public class AlarmManager extends BroadcastReceiver {
sampleDatabase appdb;
SQLiteDatabase sqldb;
Cursor cursor;
int today,prev;
Intent in;
@Override
public void onReceive(Context context, Intent intent)
{
NotificationManager manger = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon , "Yahrzeit" , System.currentTimeMillis());
in = new Intent(context,FirstAppActivity.class);
in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, in, 0);
notification.setLatestEventInfo(context, "ALERT!!!!", "Time Over" , contentIntent);
notification.defaults=Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE;
notification.flags = Notification.FLAG_INSISTENT;
appdb = new sampleDatabase(context);
sqldb = appdb.getReadableDatabase();
cursor = sqldb.query(sampleDatabase.TABLE_SEC, null, null, null, null, null, null);
cursor.moveToFirst();
Calendar cal = Calendar.getInstance();
cal.set(Calendar.SECOND, 0);
today = (int)(cal.getTimeInMillis()/1000);
Log.e("Naga","Alarm");
Log.e("today",Integer.toString(today));
prev = 0;
cursor.moveToPrevious();
while(cursor.moveToNext())
{
int dbdate = cursor.getInt(cursor.getColumnIndex(sampleDatabase.ALARMSET));
int id = cursor.getInt(cursor.getColumnIndex(sampleDatabase.ROW_ID));
Log.e("dbdate",Integer.toString(dbdate));
if((dbdate<=today)&&(dbdate>prev))
{
manger.cancelAll();
manger.notify(1, notification);
sqldb.execSQL("DELETE from " + sampleDatabase.TABLE + " where " + sampleDatabase.ROW_ID + "=" + id);
sqldb.execSQL("DELETE from "+sampleDatabase.TABLE_SEC + " where " + sampleDatabase.ROW_ID + "=" + id);
}
prev = dbdate;
}
cursor.close();
sqldb.close();
}
我的logcat视图是这样的(我已经插入了vibrate的权限,然后也出现了这个错误。):
11-14 18:09:18.000: ERROR/AndroidRuntime(757): FATAL EXCEPTION: main
11-14 18:09:18.000: ERROR/AndroidRuntime(757): java.lang.RuntimeException: Unable to start receiver com.nagainfo.firstAp.AlarmManager: java.lang.SecurityException: Requires VIBRATE permission
11-14 18:09:18.000: ERROR/AndroidRuntime(757): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2821)
11-14 18:09:18.000: ERROR/AndroidRuntime(757): at android.app.ActivityThread.access$3200(ActivityThread.java:125)
11-14 18:09:18.000: ERROR/AndroidRuntime(757): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2083)
11-14 18:09:18.000: ERROR/AndroidRuntime(757): at android.os.Handler.dispatchMessage(Handler.java:99)
11-14 18:09:18.000: ERROR/AndroidRuntime(757): at android.os.Looper.loop(Looper.java:123)
11-14 18:09:18.000: ERROR/AndroidRuntime(757): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-14 18:09:18.000: ERROR/AndroidRuntime(757): at java.lang.reflect.Method.invokeNative(Native Method)
11-14 18:09:18.000: ERROR/AndroidRuntime(757): at java.lang.reflect.Method.invoke(Method.java:521)
11-14 18:09:18.000: ERROR/AndroidRuntime(757): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-14 18:09:18.000: ERROR/AndroidRuntime(757): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-14 18:09:18.000: ERROR/AndroidRuntime(757): at dalvik.system.NativeStart.main(Native Method)
11-14 18:09:18.000: ERROR/AndroidRuntime(757): Caused by: java.lang.SecurityException: Requires VIBRATE permission
11-14 18:09:18.000: ERROR/AndroidRuntime(757): at android.os.Parcel.readException(Parcel.java:1247)
11-14 18:09:18.000: ERROR/AndroidRuntime(757): at android.os.Parcel.readException(Parcel.java:1235)
11-14 18:09:18.000: ERROR/AndroidRuntime(757): at android.app.INotificationManager$Stub$Proxy.enqueueNotificationWithTag(INotificationManager.java:274)
11-14 18:09:18.000: ERROR/AndroidRuntime(757): at android.app.NotificationManager.notify(NotificationManager.java:110)
11-14 18:09:18.000: ERROR/AndroidRuntime(757): at android.app.NotificationManager.notify(NotificationManager.java:90)
11-14 18:09:18.000: ERROR/AndroidRuntime(757): at com.nagainfo.firstAp.AlarmManager.onReceive(AlarmManager.java:52)
11-14 18:09:18.000: ERROR/AndroidRuntime(757): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2810)
11-14 18:09:18.000: ERROR/AndroidRuntime(757): ... 10 more
请帮助我。 我的清单是:
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".FirstAppActivity"
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=".addtable"
android:label="@string/app_name">
</activity>
<activity android:name=".showTime"
android:label="@string/app_name">
</activity>
<activity android:name=".edittable"
android:label="@string/app_name">
</activity>
<receiver android:name="AlarmManager">
</receiver>
<uses-permission android:name="android.permission.READ_CALENDAR"/>
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.VIBRATE"/>
</application>
答案 0 :(得分:5)
Caused by: java.lang.SecurityException: Requires VIBRATE permission
向清单添加VIBRATE权限。
<uses-permission android:name="android.permission.VIBRATE" />
您已将此标记放在应用标记中。它应该在应用程序标记之外。像这样:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="name.xml"
android:versionCode="24"
android:versionName="3.4" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.READ_CALENDAR"/>
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".FirstAppActivity"
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=".addtable"
android:label="@string/app_name">
</activity>
<activity android:name=".showTime"
android:label="@string/app_name">
</activity>
<activity android:name=".edittable"
android:label="@string/app_name">
</activity>
<receiver android:name="AlarmManager">
</receiver>
</application>