7

BroadcastReceiver を使用しようとしていますが、機能していません。この問題の解決を手伝ってください。MyReceiver.java

package com.example.broadcast_receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Log.i("[BroadcastReceiver]", "MyReceiver");

        if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
            Log.i("[BroadcastReceiver]", "Screen ON");
        }
        else if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
            Log.i("[BroadcastReceiver]", "Screen OFF");
        }
    }
}

AndroidManifest.xml

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

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <receiver android:name=".MyReceiver" 
            android:enabled="true"
            android:exported="false">
            <intent-filter>
                <action android:name="android.intent.action.SCREEN_ON"/>
                <action android:name="android.intent.action.SCREEN_OFF"/>
            </intent-filter>
        </receiver>

        <activity
            android:name="com.example.broadcast_receiver.MainActivity"
            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>

BroadcastReceiver が機能せず、ログも作成されません。この問題の解決を手伝ってください。

4

3 に答える 3

23

ブロードキャストの動的呼び出しを使用してみてください。これを試してみましたが、うまくいくでしょう...

public class MainActivity extends Activity {

    //Create broadcast object
    BroadcastReceiver mybroadcast = new BroadcastReceiver() {    
        //When Event is published, onReceive method is called
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            Log.i("[BroadcastReceiver]", "MyReceiver");

            if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
                Log.i("[BroadcastReceiver]", "Screen ON");
            }
            else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
                Log.i("[BroadcastReceiver]", "Screen OFF");
            }

        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        registerReceiver(mybroadcast, new IntentFilter(Intent.ACTION_SCREEN_ON));
        registerReceiver(mybroadcast, new IntentFilter(Intent.ACTION_SCREEN_OFF));
    }
}
于 2013-04-23T05:24:07.597 に答える
4

If you want this receiver to be called by the system, you would need to export it. You set exported = "false", change this to true or remove exported entirely and this will start working. Normally this would be insecure, but as both SCREEN_ON and SCREEN_OFF are protected-broadcasts, and you verify the actions, only more trusted system code can send them too you, so it's pretty safe.

Sadly this wont work in this case as the intents broadcast have the following flags: Intent.FLAG_RECEIVER_REGISTERED_ONLY | Intent.FLAG_RECEIVER_FOREGROUND

于 2016-12-15T21:23:42.900 に答える
3

バッテリーの値を取得してみてください:

public class Broadcast extends Activity {
    //Create broadcast object
    BroadcastReceiver mybroadcast = new BroadcastReceiver() {

        //When Event is published, onReceive method is called
        @Override
        public void onReceive(Context context, Intent intent) {
            //Get battery percentage
            int batterylevel = intent.getIntExtra("level", 0);    
            //get progressbar
            ProgressBar myprogressbar = (ProgressBar) findViewById(R.id.progressbar);
            myprogressbar.setProgress(batterylevel);
            TextView tv = (TextView) findViewById(R.id.textfield);
            //Set TextView with text
            tv.setText("Battery Level: " + Integer.toString(batterylevel) + "%");
        }
    });

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_broadcast);   
        registerReceiver(mybroadcast, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    }
} 
于 2013-04-23T04:57:53.967 に答える