1

我使用了类似于stackoverflow链接之一的代码: 如何将系统警报设置文本动态更改 为仅在锁定屏幕上设置文本,但我的应用程序在运行时崩溃,知道我缺少什么吗?

我的主要活动:

package l.a;
import java.net.ContentHandler;
import android.app.Activity;
import android.app.KeyguardManager;
import android.app.KeyguardManager.KeyguardLock;
import android.content.Context;
import android.os.Bundle;
import android.provider.Settings;


public class LockscreenActivity extends Activity {

/** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Context context= getApplicationContext();
    KeyguardManager _guard = (KeyguardManager)
 context.getSystemService(Context.KEYGUARD_SERVICE);
        KeyguardLock _keyguardLock = _guard.newKeyguardLock("KeyguardLockWrapper");
                //to disable
        _keyguardLock.disableKeyguard();


         String message ="New alarm :";
         Settings.System.putString(this.getContentResolver(),
                 Settings.System.NEXT_ALARM_FORMATTED, message);

                 //to enable
         _keyguardLock.reenableKeyguard();

}
}

我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="l.a"
 android:versionCode="1"
 android:versionName="1.0" >

  <uses-sdk android:minSdkVersion="7" />
  <uses-permission android:name="android.permission.WAKE_LOCK"/>
  <uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>

  <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".LockscreenActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

日志猫:

04-03 16:43:53.920: D/AndroidRuntime(486): Shutting down VM
04-03 16:43:53.928: W/dalvikvm(486): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
04-03 16:43:53.968: E/AndroidRuntime(486): Uncaught handler: thread main exiting due to uncaught exception
04-03 16:43:54.138: E/AndroidRuntime(486): java.lang.RuntimeException: Unable to start activity ComponentInfo{l.a/l.a.LockscreenActivity}: java.lang.SecurityException: Permission Denial: writing com.android.providers.settings.SettingsProvider uri content://settings/system from pid=486, uid=10040 requires android.permission.WRITE_SETTINGS
04-03 16:43:54.138: E/AndroidRuntime(486):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
04-03 16:43:54.138: E/AndroidRuntime(486):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
04-03 16:43:54.138: E/AndroidRuntime(486):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
04-03 16:43:54.138: E/AndroidRuntime(486):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
04-03 16:43:54.138: E/AndroidRuntime(486):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-03 16:43:54.138: E/AndroidRuntime(486):  at android.os.Looper.loop(Looper.java:123)
04-03 16:43:54.138: E/AndroidRuntime(486):  at android.app.ActivityThread.main(ActivityThread.java:4363)
04-03 16:43:54.138: E/AndroidRuntime(486):  at java.lang.reflect.Method.invokeNative(Native Method)
04-03 16:43:54.138: E/AndroidRuntime(486):  at java.lang.reflect.Method.invoke(Method.java:521)
04-03 16:43:54.138: E/AndroidRuntime(486):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
04-03 16:43:54.138: E/AndroidRuntime(486):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-03 16:43:54.138: E/AndroidRuntime(486):  at dalvik.system.NativeStart.main(Native Method)
04-03 16:43:54.138: E/AndroidRuntime(486): Caused by: java.lang.SecurityException: Permission Denial: writing com.android.providers.settings.SettingsProvider uri content://settings/system from pid=486, uid=10040 requires android.permission.WRITE_SETTINGS
04-03 16:43:54.138: E/AndroidRuntime(486):  at android.os.Parcel.readException(Parcel.java:1218)
04-03 16:43:54.138: E/AndroidRuntime(486):  at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:160)
04-03 16:43:54.138: E/AndroidRuntime(486):  at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114)
04-03 16:43:54.138: E/AndroidRuntime(486):  at android.content.ContentProviderProxy.insert(ContentProviderNative.java:445)
04-03 16:43:54.138: E/AndroidRuntime(486):  at android.content.ContentResolver.insert(ContentResolver.java:629)
04-03 16:43:54.138: E/AndroidRuntime(486):  at android.provider.Settings$NameValueTable.putString(Settings.java:464)
04-03 16:43:54.138: E/AndroidRuntime(486):  at android.provider.Settings$System.putString(Settings.java:591)
04-03 16:43:54.138: E/AndroidRuntime(486):  at l.a.LockscreenActivity.onCreate(LockscreenActivity.java:43)
04-03 16:43:54.138: E/AndroidRuntime(486):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-03 16:43:54.138: E/AndroidRuntime(486):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
04-03 16:43:54.138: E/AndroidRuntime(486):  ... 11 more
4

1 回答 1

0

好吧,通过简单地阅读 logcat,我发现了这一点:在第 4 行的末尾需要 android.permission.WRITE_SETTINGS。

于 2013-04-03T14:53:07.220 回答