广播的定义,若是是内部类,必须为静态类.html
<receiver android:name=".MainActivity$MyReciver">
<intent-filter>
<action android:name="com.qianfeng.1605"></action>
</intent-filter>
</receiver>
2.内部类在声明时必定要写成静态内部类(class关键字前加上static)。不然会抛出相似这样的异常:java
java.lang.RuntimeException: Unable to instantiate receiver com.example.brocastdemo.MainActivity$MyReceiver:
java.lang.InstantiationException:
can't instantiate class com.example.brocastdemo.MainActivity$MyReceiver; no empty constructor
static TextView tv_name;
static TextView tv_password;
public static class MyReciver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,intent.getStringExtra("name"),Toast.LENGTH_SHORT).show();
Toast.makeText(context,intent.getStringExtra("password"),Toast.LENGTH_SHORT).show();
tv_name.setText(intent.getStringExtra("name"));
tv_password.setText(intent.getStringExtra("password"));
}
}