android 广播自定义广播接收问题

2025年03月08日 00:36
有1个网友回答
网友(1):

其实没啥技术可言的,就是Android中可以自定义权限的,对于四大组件的访问加上一层保护,不多说了,直接上代码:

发送广播:

[java] view plaincopy
package com.tt.test;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((Button)findViewById(R.id.button)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent("COM.MESSAGE");
i.addCategory("receiver");
i.putExtra("message", "haha");
sendOrderedBroadcast(i, "xvtian.gai.receiver");
}
});
}
}
AndroidManifest.xml:

[html] view plaincopy



接收广播:

[java] view plaincopy
package com.tt.receiver;

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

public class Receiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
Log.d("TAG", "receiver intent:" + intent.toString());
}

}
AndroidManifest.xml

[html] view plaincopy

[html] view plaincopy