Android 从广播接收器调用活动中的方法
这是我的广播接收器类,我想在我的main活动中调用一个方法(重新连接();),同时显示“连接丢失!尝试重新连接…”吐司Android 从广播接收器调用活动中的方法,android,Android,这是我的广播接收器类,我想在我的main活动中调用一个方法(重新连接();),同时显示“连接丢失!尝试重新连接…”吐司 导入android.content.BroadcastReceiver; 导入android.content.Context; 导入android.content.Intent; 导入android.net.ConnectivityManager; 导入android.net.NetworkInfo; 导入android.widget.Toast; 公共类连接Stablizer
导入android.content.BroadcastReceiver;
导入android.content.Context;
导入android.content.Intent;
导入android.net.ConnectivityManager;
导入android.net.NetworkInfo;
导入android.widget.Toast;
公共类连接StablizerReceiver扩展BroadcastReceiver{
@凌驾
公共void onReceive(上下文、意图){
if(isConnected(context))Toast.makeText(context,“Connection builded!”,Toast.LENGTH_SHORT.show();
else Toast.makeText(上下文,“连接丢失!尝试重新连接…”),Toast.LENGTH_LONG.show();
}
公共布尔值未连接(上下文){
连接管理器cm=
(ConnectionManager)context.getSystemService(context.CONNECTIVITY_服务);
NetworkInfo activeNetwork=cm.getActiveNetworkInfo();
布尔值isConnected=activeNetwork!=null&&
activeNetwork.isConnected();
返回不连接;
}
}
创建ConnectionsTablezerReceiver类作为活动的内部类。;它会起作用的
public class Splash_screen extends Activity {
private static int SPLASH_TIME_OUT = 4000;
Global global;
SharedPreferences pref;
String base_url_final="";
private static String checkurlonetag ="sdfasdf";
private static String checkurltwotag ="sdfasdfgsdfhsfdhasdf";
// Dbhelper dbhelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
global= (Global) getApplicationContext();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.splash_screen, menu);
return true;
}
class ConnectionStablizerReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
//dont forget to register reciver permision
if(intent.getAction().equals("android.net.conn.CONNECTIVITY_CHANGE")){
//do your onreceive action
}
}
}
}
改用EventBus库来解耦代码!检查此项。使用不同的操作从广播接收器发送广播,该操作将在活动中接收广播接收器。广播接收器内部正确的方法现在有什么问题?请将您的清单放在此处并记录
public class Splash_screen extends Activity {
private static int SPLASH_TIME_OUT = 4000;
Global global;
SharedPreferences pref;
String base_url_final="";
private static String checkurlonetag ="sdfasdf";
private static String checkurltwotag ="sdfasdfgsdfhsfdhasdf";
// Dbhelper dbhelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
global= (Global) getApplicationContext();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.splash_screen, menu);
return true;
}
class ConnectionStablizerReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
//dont forget to register reciver permision
if(intent.getAction().equals("android.net.conn.CONNECTIVITY_CHANGE")){
//do your onreceive action
}
}
}
}