我正在开发一个需要永久互联网连接的应用程序.如果没有互联网连接,我希望用户退出应用程序(进入登录屏幕).我有一个网络接收器类,可以检测网络连接.我希望这个类要么终止堆栈顶部的活动,要么启动新的登录活动并删除整个历史堆栈.
问题是我无法从我的接收器类内部完成前台活动,并且当网络出现故障时无法知道用户所处的活动.如果我从这个类开始一个新的登录活动,当用户按下"返回"时,他将被带回应用程序(如果他重新连接到网络),但该应用程序未登录并崩溃.
尝试myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK || FLAG_ACTIVITY_CLEAR_TOP);从我的NetworkStateReceiver类启动新的登录活动时使用.但它不起作用,根据我的理解,这创建了一个新任务,其中唯一的活动是我开始的(登录),但旧任务与所有其他活动保持不变.
所以我需要:
- 一种从类中完成前景活动的方法
- 或者从类中启动新活动并清空活动堆栈的方法
这是接收器代码的价值:
public class NetworkStateReceiver extends BroadcastReceiver{
public void onReceive(Context context, Intent intent) {
// super.onReceive(context, intent);
Log.d("app","Network connectivity change");
if(intent.getExtras()!=null) {
Login.apiKey = null;
NetworkInfo ni=(NetworkInfo) intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO);
if(ni!=null && ni.getState()==NetworkInfo.State.CONNECTED) {
Log.i("app","Network "+ni.getTypeName()+" connected");
}
}
if(intent.getExtras().getBoolean(ConnectivityManager.EXTRA_NO_CONNECTIVITY,Boolean.FALSE) && !Login.loginActive) {
Log.d("app","There's no network connectivity");
Toast.makeText(context, "No internet connection. Logging out", Toast.LENGTH_LONG).show();
//logout
Receiver.engine(context).halt();
Receiver.mSipdroidEngine = null;
Receiver.reRegister(0);
new Thread(ChatList.runnable).interrupt();
ChatList.buddyList.clear();
Login.apiKey = null;
Log.i("Logout", "Logged out!");
Login.loggedOut = …Run Code Online (Sandbox Code Playgroud) 我一直在我的应用程序上使用谷歌的融合位置服务进行位置跟踪.我使用Android studio和它的gradle构建系统.最近我发现谷歌的位置服务存在一个大问题.
我的gradle构建文件包含这个依赖项:compile 'com.google.android.gms:play-services:+.因此,当我重建我的项目时,Android工作室下载了谷歌播放服务的最新版本,即5.2.08.但是我的设备(三星Galaxy S4)只有5.0.89版本.显然这两个版本不兼容,我得到这个错误:Google Play services out of date. Requires 5208000 but found 5089038.我无法在我的设备上更新Google Play服务.它声明它是最新的(5.0.89).
使用此解决方案,我设法使跟踪系统正常工作,但它无法在我拥有的另一台设备(W8L设备)上运行,该设备还有另一个版本的google play服务:5.0.84.
谷歌似乎发生这种情况并不常见.我如何解决这个问题,并确保谷歌不会弄乱我的应用程序.
java android location google-play-services android-gradle-plugin
我一直在开发一个跟踪用户位置的应用程序。为此,我使用了谷歌播放服务位置模块(又名融合位置)。总而言之,一切正常。
但有时,完全随机,我根本不再收到来自谷歌位置服务的位置更新!我的意思是,我的应用程序工作正常,但没有位置更新。不仅如此,如果我启动谷歌地图,它也无法修复我的位置(即使打开了 GPS)。重新安装我的应用程序并不能解决问题。在我手机上的 3 个出租车应用程序中,其中 2 个可以精确定位我的位置。(很可能他们正在使用旧的 LocationManager 来获取设备的位置)。
我发现要解决这个问题,我必须重新启动我的设备。但这非常令人不安,我不能指望我的用户在位置更新出现问题时重新启动他们的设备。(尽管在过去的两个月里,这种情况只发生在我身上大约六次)
那么:有谁知道这个问题并且有解决方法吗?我的应用程序是否会导致 Google Play 服务崩溃?
我试图避免必须手动授予使用 Espresso 构建的每个 UI 测试的权限。
我试过了:
@Rule
public GrantPermissionRule permissionRule = GrantPermissionRule.grant(
android.Manifest.permission.INTERNET,
android.Manifest.permission.READ_PHONE_STATE,
android.Manifest.permission.ACCESS_NETWORK_STATE,
android.Manifest.permission.ACCESS_FINE_LOCATION.....)
Run Code Online (Sandbox Code Playgroud)
和
@Before
public void grantPhonePermission()
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
getInstrumentation().getUiAutomation().executeShellCommand(
"pm grant " + getTargetContext().getPackageName()
+ " android.permission.ACCESS_FINE_LOCATION");
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的大部分 gradle 文件:
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion '26.0.2'
defaultConfig {
minSdkVersion 14
targetSdkVersion 23
testInstrumentationRunner
'android.support.test.runner.AndroidJUnitRunner'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.txt'
debuggable false
jniDebuggable false
buildConfigField "boolean", "UseProduction", "true"
}
qa {
initWith …Run Code Online (Sandbox Code Playgroud)