我想從Broadcast Receiver調用Main Activity中的方法。這是我的MainActivity來自BroadcastReceiver的MainActivity中的調用方法 - Android
public class MainActivity extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void DisplayConn(){
if(isNetworkStatusAvailable(getApplicationContext())) {
Toast.makeText(getApplicationContext(), "internet is available", Toast.LENGTH_LONG).show();
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Error");
builder.setMessage("No Network Connection").setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
}
,這是我的BroadcastReceiver
public class ConnectionReceiver extends BroadcastReceiver {
public ConnectionReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
MainActivity myAct = new MainActivity();
myAct.DisplayConn();
}
}
所以,每次我廣播接收有它會叫我的MainActivity的方法。提前致謝。
@ jvpintang:你爲什麼不在你的活動中註冊廣播? – dex