0

通知からアクティビティを呼び出すと、画面が暗くなるようにしようとしています。私が使用しているコードは次のとおりです。

WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 0.01f;
getWindow().setAttributes(lp);  

たぶん、私はここで何か間違ったことをしているのかもしれません。

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;


public class NoteMe extends Activity {
/** Called when the activity is first created. */
NotificationManager nm;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String title = "Example text";
    String contentText = "Full hello world!";
    String text = "Starting Notification!";

    nm = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);
    long when = System.currentTimeMillis();
    int icon = R.drawable.ic_launcher;

    Intent intent = new Intent(getBaseContext(), MainActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(
            getBaseContext(), 0, intent, 0);

    Notification notification = new Notification(icon, text, when);
    notification.setLatestEventInfo(getBaseContext(), title, contentText,
            contentIntent);
    notification.flags = Notification.FLAG_ONGOING_EVENT;
    int NOTIFICATION_ID = 10;
    nm.notify(NOTIFICATION_ID, notification);
}
}

リクエストしたコードはこちら

4

3 に答える 3

0

投稿したコードは、アプリケーションの画面を暗くするだけです。それがあなたの望みなら、それでいいのです。ただし、電話の画面を暗くしたい場合は、システム設定System.SCREEN_BRIGHTNESSを変更する必要があります

    // turn on manual control of system screen brightness
    Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);

    final int dimBrightnessValue = 96;  // valid values 0-255
    // change the brightness value
    System.putInt(getContentResolver(), System.SCREEN_BRIGHTNESS, dimBrightnessValue);

AndroidManifest.xml に以下を追加する必要もあります。

<!-- Needed for changing screen brightness -->
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
于 2012-07-19T18:51:42.957 に答える
0

あなたのコードは 100% 正しいです。2 つの可能性があり、最初の可能性が最も高い

1)活動は通知から取り消されていません。保留中のインテントを含む通知から発火する特別な方法があると思います。それが問題だと思います。念のため、onCreate と onStart にいくつかのブレークポイントを設定して、呼び出されるかどうかを確認してください。または、コードの近くにログ ステートメントを配置して、呼び出されるかどうかを確認します。私はそれが呼び出されることはないと思います。

2) あなたが行っている調整 0.1f は目に見える変化ではありません。

これを確認して、通知が受信されていることを確認してください。

http://www.vogella.com/articles/AndroidNotifications/article.html

私が見る違いの1つは、 getContextBase() と this です

そうでない場合は、 MainActivity.class マニフェスト宣言が正しくない可能性があります。

于 2012-07-19T18:52:32.970 に答える
0

このようなことを試してください - 属性を設定した後に画面を更新してください。

    WindowManager.LayoutParams lp = getWindow().getAttributes();
    lp.screenBrightness =0.01f
    getWindow().setAttributes(lp);

    startActivity(new Intent(this,RefreshScreen.class));

したがって、画面を更新する際のハックの 1 つは、ダミー アクティビティを開始することであり、そのダミー アクティビティを作成して、finish(); を呼び出すことです。明るさの変化が効果を発揮します。

于 2012-07-19T18:44:06.407 に答える