80,350
社区成员
发帖
与我相关
我的任务
分享
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("MyBroadcastReceiver", "onReceive: ");
Toast.makeText(context,"this is myReceiver",Toast.LENGTH_SHORT).show();
}
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.zoukeqing.helloworld">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<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/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="com.example.zoukeqing.helloworld.MY_BROADCAST"/>
</intent-filter>
</receiver>
</application>
</manifest>
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("MainActivity", "onCreate: ");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button)findViewById(R.id.btn_broadcast);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendBroadcast(new Intent("com.example.zoukeqing.helloworld.MY_BROADCAST"));
}
});
}
}