廣播的定義,若是是內部類,必須爲靜態類.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"));
}
}