我收到广播接收器的弃用声明的警告.
<!-- NETWORK RECEIVER... -->
<receiver android:name=".utils.NetworkUtils" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
警告:
对于面向N及更高版本的应用,不推荐为android.net.conn.CONNECTIVITY_CHANGE声明广播接收器.通常,应用程序不应该依赖此广播,而应使用JobScheduler或GCMNetworkManager.
没有弃用的方法有没有其他方法可以使用它?
android deprecated intentfilter broadcastreceiver android-networking
我是android的新手.我哪知道之间的区别Intent和BroadcastReceiver.我有更多的困惑BroadcastReceiver比Intent.
请帮帮我.简单的代码将有所帮助.
这是我的网络请求示例
networkAPI.postData(myData).enqueue(new Callback<MyResponse>() {
@Override
public void onResponse(Call<MyResponse> call, Response<MyResponse> response) {
if (response.code() == 201) {
// success
}
}
@Override
public void onFailure(Call<MyResponse> call, Throwable t) {
}
});
Run Code Online (Sandbox Code Playgroud)
这是我应该只拨打一次的请求,因为我有一次可用的优惠券代码.如果我立即关闭我的网络连接或者在调用此方法后立即断开连接,那么它会移动到onFailure带有IOExceptionthrowable的方法,这通常是指连接错误,但重点是发布到服务器的数据和使用的凭证代码,问题是onFailure方法在这里被称为.
我确信它不是解析失败,因此我也尝试使用它Void.为什么onResponse即使在发布数据后也没有调用.如何克服这种情况?
我想在broadcastReceiver中检查设备是否已连接.下面是我的代码:
public boolean isOnline(Context context) {
NetworkInfo info = (NetworkInfo) ((ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (info == null || !info.isConnected()) {
Log.e("UpdateDataReceiver","info: "+info);
return false;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
我的代码问题: 当BroadcastReceiver在后台触发时(当app在后台时),上面的函数返回false(即使连接了wifi),当app在前台时它返回true.
info:NetworkInfo:type:WIFI [],state:DISCONNECTED/BLOCKED,reason :( unspecified),extra:(none),漫游:false,failover:false,isAvailable:true,isConnectedToProvisioningNetwork:false,simId:0
设备信息:Redmi Note
我知道如何在我的应用程序使用活动打开时检查互联网连接.但是,当我的应用程序未运行时,如何检查服务中的连接?
我使用这种方法来 ping 谷歌服务器,这样我就可以检查活动internet连接,但我发现它并不适用于所有设备。
我尝试过使用其他方法,例如HttpURLConnectionandURLConnection但即使我已连接,它们也会返回 false。
任何适用于所有设备的想法或解决方案。提前致谢。我将连续发布我已经尝试过的内容。
方法一:
public static Boolean isOnline() {
try {
Process p1 = java.lang.Runtime.getRuntime().exec("ping -c 1 8.8.8.8");
int returnVal = p1.waitFor();
return (returnVal == 0);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
我在本地和谷歌服务器上尝试过这个,它产生了完美的结果。问题是它不适用于所有设备。
方法二:
public boolean isConnected() {
boolean connectivity;
try {
URL url = new URL("www.google.com");
URLConnection conn = url.openConnection();
conn.setConnectTimeout(5000);
conn.connect();
connectivity = true;
} catch (Exception e) {
connectivity …Run Code Online (Sandbox Code Playgroud) 我已经创建了在开始时检查互联网连接的代码,但我希望它在后台继续检查互联网连接并在连接丢失时通知用户。我是 android 新手,所以请您编写正确的代码并帮助我。这段代码工作正常,我只是想让它在后台运行以检查互联网。
public class isNetworkAvailable extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_is_network);
;
if(!isNetworkAvailable()){
//Create an alert dialog
AlertDialog.Builder Checkbuilder = new AlertDialog.Builder(isNetworkAvailable.this);
Checkbuilder.setIcon(R.drawable.error);
Checkbuilder.setTitle("Error!");
Checkbuilder.setMessage("Check Your Internet Connection.");
//Builder Retry Button
Checkbuilder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//Restart The Activity
Intent intent = getIntent();
finish();
startActivity(intent);
}
});
Checkbuilder.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
}) ;
AlertDialog alert=Checkbuilder.create();
alert.show();
} …Run Code Online (Sandbox Code Playgroud) 当用户解锁屏幕时,我的应用程序需要进行祝酒,因此我注册了一个来BroadcastReceiver获取ACTION_USER_PRESENT清单中的意图,如下所示:
<receiver
android:name=".ScreenReceiver" >
<intent-filter>
<action
android:name="android.intent.action.USER_PRESENT"/>
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
然后我定义了一个这样的类:
package com.patmahoneyjr.toastr;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class ScreenReceiver extends BroadcastReceiver {
private boolean screenOn;
private static final String TAG = "Screen Receiver";
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
screenOn = true;
Intent i = new Intent(context, toastrService.class);
i.putExtra("screen_state", screenOn);
context.startService(i);
Log.d(TAG, " The screen turned on!");
} else if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
screenOn = false;
}
}
}
Run Code Online (Sandbox Code Playgroud)
但由于某种原因,Log …
美好的一天,我有一个应用程序有2个活动:主页和详细信息页面.
当有互联网连接时,用户可以从主页面导航到详细信息页面.没有互联网连接,他不能这样做.
问题是:当我在详细信息页面并关闭wifi时,我想完成此活动,我该如何实现此功能?我检查了主要的活动类是这样的:
Run Code Online (Sandbox Code Playgroud)private boolean isNetworkAvailable() { ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); return activeNetworkInfo != null && activeNetworkInfo.isConnected(); }
当我用互联网启动应用程序或没有它时,它的工作正常,但是当我在运行时关闭wifi时,它不起作用.
无论如何,谢谢!
在Firebase Job调度程序中,可以检测到何时连接到网络,但是如何检测何时断开与网络的连接?
Job myJob = dispatcher.newJobBuilder()
.setService(MyJobService.class)
.setTag("my-unique-tag")
.setConstraints(
// only run on an unmetered network
Constraint.ON_UNMETERED_NETWORK,
// only run when the device is charging
Constraint.DEVICE_CHARGING
)
.build();
dispatcher.mustSchedule(myJob);
Run Code Online (Sandbox Code Playgroud)
这将检测到何时连接到Unmetrered网络,无论何时从所有网络断开连接,我都想启动服务
当应用程序未运行时收到推送通知并且用户按下通知时,它会消失并且日志显示:
2019-10-22 12:42:45.747 23260-23260/de.app.test.staging E/FirebaseMessaging: Notification pending intent canceled
Run Code Online (Sandbox Code Playgroud)
这是应该作为启动器活动启动的 SplashActivity:
<activity
android:name="de.app.test.login.SplashActivity"
android:screenOrientation="portrait"
android:exported="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)
这里有什么问题?
当互联网可用且应用已经关闭时,是否有可能发送数据?假设我有app必须发送一些数据,但用户没有互联网连接.他关闭了应用程序,之后他打开了wifi.现在我想发送以前等待发送的数据.我能以某种方式做到吗?
我做了一个表单应用程序,将数据发送到我的mysql服务器.一切都很好,但我有一个问题:如果我的应用程序没有互联网,我应该使用什么...如果我有互联网,用户输入数据并提交,但如果5或10分钟我没有互联网访问并且用户仍然发送数据.我是否丢失了这些数据,或者有没有办法保存数据?当互联网再次激活时,所有保存的数据都会传输到我的mysql服务器......?
我听说过JSON Parser,但我不确切知道它是如何工作的......