-1

我有两个实现接收器。

第一次实现:



    public class ApplicationStartListener extends BroadcastReceiver {
        Context mContext;
        @Override
        public void onReceive(Context context, Intent intent) {
                mContext = context;
                Toast.makeText(mContext, "FUI", Toast.LENGTH_LONG).show();
                Log.v("Server application FUI", intent.getAction());
        }

此实现不起作用。

二次实施:



    public class TestActiv extends Activity{

        public void onCreate(Bundle bundle){
            super.onCreate(bundle);
            setContentView(R.layout.main);
            final IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
            intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
            intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
            intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
            intentFilter.addAction(Intent.ACTION_SCREEN_ON);
            intentFilter.addAction(Intent.ACTION_CAMERA_BUTTON);



            BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    Log.v("Server applic", "FUI: " + intent.getAction());
                }
            };
            registerReceiver(broadcastReceiver, intentFilter);
        }
    }

为什么不能先实现BroadcastReceiver?

4

2 回答 2

0

To register this receiver this cannot be inside the AndroidManifest.xml, register at runtime

Register this receiver in any service.

IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
于 2013-03-24T08:36:18.713 回答
0

我不需要意图 SCREEN_ON 和 SCREEN_OFF。只需要 PACKAGE_CHANGED。它在设备上运行应用程序时有效吗?


    public class TestActiv extends Activity{

        public void onCreate(Bundle bundle){
            super.onCreate(bundle);
            setContentView(R.layout.main);
            final IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);  


            BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    Log.v("Server applic", "FUI: " + intent.getAction());
                }
            };
            registerReceiver(broadcastReceiver, intentFilter);
        }
    }
于 2013-03-24T17:16:30.003 回答