静态变量不返回正确? [英] Static variables not returning correctly?

查看:131
本文介绍了静态变量不返回正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要建一个小应用程序,这是正确的,现在他们的服务和活动的工作相当不错。

虽然,我试图挽救登录后一些静态信息(如出现服务已经启动了吗?)到一个静态布尔,isRunning。这将在OnCreate()中设置为true,但当我后来把它从活动中,它始终返回false。

从服务:

 公共静态布尔isRunning = FALSE;公共无效的onCreate(){
        super.onCreate();
    isRunning = TRUE;
}

有谁知道为什么不起作用?我已经使用一些日志弄清楚发生了什么尝试,但我似乎无法推测出来。

从活动

 公共无效onResume(){
        super.onResume();
        如果(mIsBound){
            Log.i(LOG_TAG,恢复:服务正在运行);
            如果(Service.isRunning){
                Log.e(LOG_TAG,服务正在运行!);
            }其他{
                Log.e(LOG_TAG,服务没有运行!);
            }
        }其他{
            Log.i(LOG_TAG,恢复:服务未运行);
        }
        StopCheck.setChecked(mIsBound);
    }

该mIsBound是正在由活动创建绑定到服务(我希望它重新绑定,但这似乎是不可能的)是什么,它​​是在当前状态可靠。但不是外面的活动,这就是我想要使用静态变量是什么。该Service.isRunning应该返回true,如果mIsBound等于为true。然而,在我的日志小测试的结果是恢复:服务正在运行。其次是服务未运行

任何建议或疑问都非常AP preciated。

的要求:AndroidManifest.xml中

 <?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.somnu.ServiceTest
    安卓版code =1
    机器人:=的versionName1.0>    <采用-SDK安卓的minSdkVersion =7/>    <使用许可权的android:NAME =android.permission.ACCESS_FINE_LOCATION/>
    <使用许可权的android:NAME =android.permission.ACCESS_COARSE_LOCATION/>
    <使用许可权的android:NAME =android.permission.INTERNET对/>    <应用
        机器人:图标=@绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME>
        <活动
            机器人:登录NAME =
            机器人:可调试=真
            机器人:标签=@字符串/ APP_NAME>
            &所述;意图滤光器>
                <作用机器人:名字=android.intent.action.MAIN/>
                <类机器人:名字=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
        <活动
            机器人:活动名称=
            机器人:可调试=真
            机器人:标签=@字符串/ APP_NAME>
        < /活性GT;        <服务
            机器人:服务NAME =
            机器人:工艺=:服务>
        < /服务>    < /用途>< /清单>


解决方案

删除安卓过程


  

的过程,其中服务是运行的名称。通常情况下,在应用程序创建的默认过程中的应用程序运行的所有组件。它具有相同的名称作为应用程序包。元素的process属性可以为所有的组件不同的默认。但是组件可以覆盖默认有自己的进程属性,让您送$ P $垫在多个流程应用程序。


 如果分配给该属性的名称以冒号(':'),一个新的进程,私有的应用程序,在需要时创建和服务,在这一过程中运行。如果进程名称以小写字母开头,该服务将在该名字的全局进程中运行,只要它有权这样做。这使得在不同应用程序的组件共享一个过程,减少资源的使用。

I'm building a little app, that's working quite nicely with a service and activity right now.

Although, I'm trying to save some static information upon login (like has the service been started already?) into a static boolean, isRunning. It will set to true upon onCreate(), but when I call it later from the activity, it always returns false.

From the service:

public static boolean isRunning = false;

public void onCreate() {
        super.onCreate();
    isRunning = true;
}

Does anyone know why this doesn't work? I've tried using some logs to figure out what's happening but I can't seem to figure it out.

From the activity

public void onResume() {
        super.onResume();
        if(mIsBound) {
            Log.i(LOG_TAG, "Resuming: Service is running");
            if(Service.isRunning) {
                Log.e(LOG_TAG, "SERVICE IS RUNNING!");
            } else {
                Log.e(LOG_TAG, "SERVICE IS NOT RUNNING!");
            }
        } else {
            Log.i(LOG_TAG, "Resuming: Service NOT running");
        }
        StopCheck.setChecked(mIsBound);
    }

The mIsBound is what is being created by the activity to bind to the service (I wanted it to rebind, but that seems to be impossible), and it's reliable in its current state. But not outside of that activity, that's what I want to use the static variable for. The Service.isRunning should return true, if mIsBound equals to true. Yet the result of that little test in my log is "Resuming: Service is running" followed by "SERVICE IS NOT RUNNING".

Any suggestions or questions are very much appreciated.

AS REQUESTED: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.somnu.ServiceTest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="7" />

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".Login"
            android:debuggable="true"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Activity"
            android:debuggable="true"
            android:label="@string/app_name" >
        </activity>

        <service
            android:name=".Service"
            android:process=":Service" >
        </service>

    </application>

</manifest>

解决方案

Remove android:process

The name of the process where the service is to run. Normally, all components of an application run in the default process created for the application. It has the same name as the application package. The element's process attribute can set a different default for all components. But component can override the default with its own process attribute, allowing you to spread your application across multiple processes.

If the name assigned to this attribute begins with a colon (':'), a new process, private to the application, is created when it's needed and the service runs in that process. If the process name begins with a lowercase character, the service will run in a global process of that name, provided that it has permission to do so. This allows components in different applications to share a process, reducing resource usage. 

这篇关于静态变量不返回正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆