定义广播接收类
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"));
}
}); }
}
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"));
}
}); }
}
解决方案 »
- android 获取临近基站信息(注意:是周围多个基站信息)
- 有没有可能在android上直接执行一个linux程序,而不是利用lib和so库?
- Android Read-only file system
- 请教一个算法问题
- 怎样动态更新gridview的某个item
- Android工程引入第三方jar包报错
- 各位大神,有没有处理过下面的异常,一般是怎么导致的
- Android WindowManager.addView中的布局能用Fragment切换界面吗?
- AppBarLayout中使用fitsSystemWindows无效
- 我的电脑用快捷键alt + insert 调不出来方法是怎么回事?
- eclipse的activity_main.xml出现The SDK layoutlib version is not supported
- 怎么调用原生api给微信分享图片?
android:enabled="true"
android:exported="true"
http://blog.csdn.net/u011043551/article/details/68962708