如何以编程方式设置广播接收器属性?
How to set broadcast receiver attributes programmatically?
我正在我的应用程序中广播一个意图并使用广播接收器接收它。我可以处理广播和接收。没问题。但是,我想以编程方式完全注册接收器,而不是在清单文件中进行注册。请注意,在清单文件中,接收者有两个属性 android:enabled="true"
和 android:exported="false"
。我需要知道,当我以编程方式注册接收器时,如何具体设置这两个属性?
我的 AndroidManifest.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mybroadcastapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyBroadcastApplication">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="false">
</receiver>
</application>
</manifest>
我的 MainActivity.java 文件:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
MyBroadcastReceiver myReceiver;
IntentFilter intentFilter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myReceiver = new MyBroadcastReceiver();
intentFilter = new IntentFilter();
intentFilter.addAction("com.example.mybroadcastapplication.EXPLICIT_INTENT");
findViewById(R.id.button1).setOnClickListener(this);
}
public void broadcastIntent() {
Intent intent = new Intent();
intent.setAction("com.example.mybroadcastapplication.EXPLICIT_INTENT");
getApplicationContext().sendBroadcast(intent);
}
@Override
protected void onPostResume() {
super.onPostResume();
registerReceiver(myReceiver, intentFilter);
}
@Override
protected void onStop() {
super.onStop();
unregisterReceiver(myReceiver);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
broadcastIntent();
break;
default:
}
}
}
我的 MyBroadcastReceiver.java 文件:
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() != null && intent.getAction().equals("com.example.mybroadcastapplication.EXPLICIT_INTENT"))
Toast.makeText(context, "Explicit intent received.", Toast.LENGTH_LONG).show();
}
}
此致
如果您以编程方式 registering/unregistering,则无需在清单中声明 BroadcastReceiver
。如果您希望它们从外部触发器实例化(例如,在设备启动时,或从警报管理器等),您只需要在清单中声明 BroadcastReceiver
s
我正在我的应用程序中广播一个意图并使用广播接收器接收它。我可以处理广播和接收。没问题。但是,我想以编程方式完全注册接收器,而不是在清单文件中进行注册。请注意,在清单文件中,接收者有两个属性 android:enabled="true"
和 android:exported="false"
。我需要知道,当我以编程方式注册接收器时,如何具体设置这两个属性?
我的 AndroidManifest.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mybroadcastapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyBroadcastApplication">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="false">
</receiver>
</application>
</manifest>
我的 MainActivity.java 文件:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
MyBroadcastReceiver myReceiver;
IntentFilter intentFilter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myReceiver = new MyBroadcastReceiver();
intentFilter = new IntentFilter();
intentFilter.addAction("com.example.mybroadcastapplication.EXPLICIT_INTENT");
findViewById(R.id.button1).setOnClickListener(this);
}
public void broadcastIntent() {
Intent intent = new Intent();
intent.setAction("com.example.mybroadcastapplication.EXPLICIT_INTENT");
getApplicationContext().sendBroadcast(intent);
}
@Override
protected void onPostResume() {
super.onPostResume();
registerReceiver(myReceiver, intentFilter);
}
@Override
protected void onStop() {
super.onStop();
unregisterReceiver(myReceiver);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
broadcastIntent();
break;
default:
}
}
}
我的 MyBroadcastReceiver.java 文件:
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() != null && intent.getAction().equals("com.example.mybroadcastapplication.EXPLICIT_INTENT"))
Toast.makeText(context, "Explicit intent received.", Toast.LENGTH_LONG).show();
}
}
此致
如果您以编程方式 registering/unregistering,则无需在清单中声明 BroadcastReceiver
。如果您希望它们从外部触发器实例化(例如,在设备启动时,或从警报管理器等),您只需要在清单中声明 BroadcastReceiver
s