当我的互联网连接关闭时,广播接收器不起作用,但是当我的互联网连接时,它的工作正常。
我已在我的应用的安全设置中启用了自动启动功能。我也重新启动了Redmi / note3,在一些redmi2中它运行正常。
无论是离线还是在线,但在某些redmi2手机中它完全无法正常工作
public void timerTask(final Context ctx){
Log.e("calling...","timer...");
Timer t= new Timer();
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
try {
Log.e("in try","catch...");
ConnectivityManager connectivityManager = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
/*if (connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED ||
connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED)*/
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isAvailable()){
//we are connected to a network
Log.e("timeTask","executing...");
try{my_details = new Database_Check_In_Helper(ctx);}catch (NullPointerException npe){npe.printStackTrace();}
Cursor res = my_details.call_log1();
// StringBuffer buffer = new StringBuffer();
while (res.moveToNext()) {
String phNum = res.getString(0);
String callType = res.getString(1);
String callDate = res.getString(2);
String callDuration = res.getString(3);
String phoneId = res.getString(4);
// String dtStart = "2010-10-15T09:27:37Z";
/* Date date = null;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
try {
date = format.parse(callDate);
Log.e("datefor",""+date);
*//*System.out.println(date);*//*
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
DateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
Date date=null;
try {
date = (Date) formatter.parse(callDate);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
String formatedDate = cal.get(Calendar.DATE) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.YEAR);
Log.e("formatedDate",""+formatedDate);
}catch (ParseException pe){pe.printStackTrace();}
/* SimpleDateFormat sdf3 = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
Date d1 = null;
try{
d1 = sdf3.parse(callDate);
}catch (Exception e){ e.printStackTrace(); }*/
Log.e("data are",phNum+" "+callType+" "+date+" "+callDuration+" "+phoneId);
String url = "http://www.rentmystay.com/m/call_tracking_log";
ServiceNetworkConnection snc = new ServiceNetworkConnection(phNum, callType, date, callDuration, phoneId);
snc.execute(url);
}
}else{
Log.e("internet","not connected");
}
}catch (NullPointerException npe){
Log.e("npe ","come under excep");
npe.printStackTrace();
}
}
},0,5000);
}
答案 0 :(得分:0)
显然,在检查互联网连接时,您应该检查activeNetwork的isConnected()方法。
ConnectivityManager connectivityManager = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected()){
// Network Active
}else{
// Network Inactive
}