IBM Worklight - 无法在Android中获得网络信号强度

时间:2014-05-08 14:41:38

标签: java android ibm-mobilefirst telephony

以下是我的Java实现。我的想法是在编辑文本中显示myUtilString,以便在WL.NativePage中查看混合Android应用程序中的信号强度,但我什么都没得到。

我在真实设备上测试了应用程序,更改为2G,3G和LTE,但我没有收到任何内容。我已经实现了一个名为onSignalStrengthsChanged的函数,但我无法弄清楚出了什么问题。

在LogCat中没有任何兴趣;也许onSignalStrengthsChanged永远不会被解雇。

package com.AndroidShowNativePage;

import android.app.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import android.app.Activity; 
import android.content.Context; 
import android.telephony.gsm.GsmCellLocation; 
import android.telephony.PhoneStateListener; 
import android.telephony.SignalStrength; 
import android.telephony.TelephonyManager; 
import android.telephony.CellLocation; 
import android.webkit.WebView; 

public class HelloNative extends Activity {

    EditText editText = null;
    EditText editText1 = null;
    String myUtilString = "";

    private TelephonyManager tm ;
    private DroidPhoneStateListener listener;
    private GsmCellLocation location;

    public void GSM(Activity activity, WebView view) {
        tm = (TelephonyManager) activity
        .getSystemService(Context.TELEPHONY_SERVICE);
        listener = new DroidPhoneStateListener(view);
        location = new GsmCellLocation();
        } 

    public void listen() { 
    tm.listen(listener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
    //TODO 
    //tm.listen(listener, PhoneStateListener.LISTEN_SIGNAL_STRENGTH); 
    //tm.listen(listener, PhoneStateListener.LISTEN_CELL_LOCATION); 
    //tm.listen(listener, PhoneStateListener.LISTEN_DATA_ACTIVITY); 
    } 

    public void unlisten() { 
    tm.listen(listener, PhoneStateListener.LISTEN_NONE);
    } 

    public int getCid() { 
    return location.getCid();
    } 

    public int getLac() { 
    return location.getLac();
    } 

    /* 
    * ???? public int getPsc() { return location.getPsc(); } 
    */ 
    private class DroidPhoneStateListener extends PhoneStateListener {

        private WebView view;

        DroidPhoneStateListener(WebView view) {
        this.view = view;
        } 

        @Override 
        public void onCellLocationChanged(CellLocation location) {
            myUtilString = ( "onCellLocationChanged: " + location.toString());
        // view.loadUrl("javascript:droid.Accelerometer.onUpdate(); 
        } 

        @Override 
        public void onDataConnectionStateChanged(int state, int networkType) {
            myUtilString =( "onDataConnectionStateChanged: " + state + ":" + networkType);
        }

        @Override 
        public void onSignalStrengthsChanged(SignalStrength s) {
        super.onSignalStrengthsChanged(s);
        String r = s.getGsmSignalStrength() + ":" + s.getCdmaDbm() + ":"
        + s.getCdmaEcio() + ":" + s.getEvdoDbm() + ":"
        + s.getEvdoEcio() + ":" + s.getEvdoSnr() + ":"
        + s.getGsmBitErrorRate();
            myUtilString = ("hello Change('" + r.toString()+ "');");
        } 

        @Override 
        public void onSignalStrengthChanged(int asu) {
            myUtilString = ( "onSignalStrengthChanged: " + asu);
        } 

} 




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String name = getIntent().getStringExtra("nameParam");

        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        setContentView(linearLayout);

        TextView textView1 = new TextView(this);
        textView1.setText("Name received from JavaScript :: " + name);

        TextView textView2 = new TextView(this);
        textView2.setText("Enter the phone number");

        editText = new EditText(this);
        editText.setText("1234567890");

        editText1 = new EditText(this);
        editText1.setText("any measure on dBd");

        Button submitButton = new Button(this);
        submitButton.setText("Return to the Web App");
        submitButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                String phoneNumber = editText.getText().toString();
                Intent phoneNumberInfo = new Intent();
                phoneNumberInfo.putExtra("phoneNumber", phoneNumber);
                setResult(RESULT_OK, phoneNumberInfo);
                finish();
            }
        });

        Button submitMeasure = new Button(this);
        submitMeasure.setText("Go measure");
        submitMeasure.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                editText1.setText( myUtilString );

            }
        });

        linearLayout.addView(textView1);
        linearLayout.addView(textView2);
        linearLayout.addView(editText);

        linearLayout.addView(submitButton);
        linearLayout.addView(submitMeasure);
        linearLayout.addView(editText1);
    }

}

这里是LogCat:

05-08 12:06:30.140: D/SoftKeyboardDetect(1078): Ignore this event
05-08 12:06:31.190: D/WLDroidGap(1078): Started copying files to local storage...
05-08 12:06:48.800: D/dalvikvm(1078): GC_FOR_ALLOC freed 494K, 15% free 3318K/3896K, paused 35ms, total 35ms
05-08 12:06:59.950: D/dalvikvm(1078): GC_FOR_ALLOC freed 370K, 16% free 3319K/3908K, paused 34ms, total 34ms
05-08 12:07:10.540: D/WLDroidGap(1078): Finished copying files to local storage...
05-08 12:07:10.550: D/WLDroidGap(1078): no need to check web resource integrity
05-08 12:07:10.690: D/CordovaWebView(1078): >>> loadUrl(file:///data/data/com.AndroidShowNativePage/files/www/skinLoader.html)
05-08 12:07:10.690: D/PluginManager(1078): init()
05-08 12:07:10.730: D/CordovaWebView(1078): >>> loadUrlNow()
05-08 12:07:12.560: D/CordovaActivity(1078): onMessage(onPageStarted,file:///data/data/com.AndroidShowNativePage/files/www/skinLoader.html)
05-08 12:07:14.610: D/CordovaWebViewClient(1078): onPageFinished(file:///data/data/com.AndroidShowNativePage/files/www/skinLoader.html)
05-08 12:07:14.610: D/CordovaActivity(1078): onMessage(onPageFinished,file:///data/data/com.AndroidShowNativePage/files/www/skinLoader.html)
05-08 12:07:16.650: D/CordovaActivity(1078): onMessage(spinner,stop)
05-08 12:07:17.250: D/CordovaActivity(1078): onMessage(spinner,stop)
05-08 12:07:17.260: W/PluginManager(1078): THREAD WARNING: exec() call to App.show blocked the main thread for 21ms. Plugin should use CordovaInterface.getThreadPool().
05-08 12:07:17.400: D/CordovaNetworkManager(1078): Connection Type: 3g
05-08 12:07:17.400: D/CordovaActivity(1078): onMessage(networkconnection,3g)
05-08 12:07:17.420: D/CordovaNetworkManager(1078): Connection Type: 3g
05-08 12:07:17.660: D/dalvikvm(1078): GC_FOR_ALLOC freed 458K, 14% free 3365K/3908K, paused 36ms, total 39ms
05-08 12:07:17.710: W/PluginManager(1078): THREAD WARNING: exec() call to Utils.writePref blocked the main thread for 95ms. Plugin should use CordovaInterface.getThreadPool().
05-08 12:07:17.780: W/PluginManager(1078): THREAD WARNING: exec() call to Utils.writePref blocked the main thread for 35ms. Plugin should use CordovaInterface.getThreadPool().
05-08 12:07:17.790: D/CordovaWebView(1078): >>> loadUrl(file:///data/data/com.AndroidShowNativePage/files/www/default/AndroidShowNativePage.html)
05-08 12:07:17.810: D/PluginManager(1078): init()
05-08 12:07:17.810: W/PluginManager(1078): THREAD WARNING: exec() call to Utils.loadSkin blocked the main thread for 29ms. Plugin should use CordovaInterface.getThreadPool().
05-08 12:07:17.820: D/CordovaWebView(1078): >>> loadUrlNow()
05-08 12:07:18.020: D/CordovaActivity(1078): onMessage(onPageStarted,file:///data/data/com.AndroidShowNativePage/files/www/default/AndroidShowNativePage.html)
05-08 12:07:23.320: D/CordovaActivity(1078): onMessage(spinner,stop)
05-08 12:07:23.370: D/CordovaNetworkManager(1078): Connection Type: 3g
05-08 12:07:23.400: D/CordovaNetworkManager(1078): Connection Type: 3g
05-08 12:07:23.400: D/CordovaActivity(1078): onMessage(networkconnection,3g)
05-08 12:07:24.010: D/CordovaWebViewClient(1078): onPageFinished(file:///data/data/com.AndroidShowNativePage/files/www/default/AndroidShowNativePage.html)
05-08 12:07:24.010: D/CordovaActivity(1078): onMessage(onPageFinished,file:///data/data/com.AndroidShowNativePage/files/www/default/AndroidShowNativePage.html)
05-08 12:07:24.240: I/Choreographer(1078): Skipped 32 frames!  The application may be doing too much work on its main thread.
05-08 12:07:24.440: D/AndroidShowNativePage(1078): wlclient init started
05-08 12:07:24.530: D/AndroidShowNativePage(1078): Read cookies: null
05-08 12:07:24.560: D/AndroidShowNativePage(1078): CookieMgr read cookies: {}
05-08 12:07:24.870: W/AndroidShowNativePage(1078): Your application is using the WL.OptionsMenu API. Note that, if your application targets Android 3.0 (API level 11) or higher, WL.OptionsMenu might have no effect, depending on the device.
05-08 12:07:24.970: W/PluginManager(1078): THREAD WARNING: exec() call to DeviceAuth.getDeviceUUID blocked the main thread for 24ms. Plugin should use CordovaInterface.getThreadPool().
05-08 12:07:25.000: D/AndroidShowNativePage(1078): addDeviceIDHeader deviceIDSuccessCallback
05-08 12:07:25.050: W/PluginManager(1078): THREAD WARNING: exec() call to Utils.writePref blocked the main thread for 21ms. Plugin should use CordovaInterface.getThreadPool().
05-08 12:07:25.080: D/AndroidShowNativePage(1078): connectOnStartup finalizeInit
05-08 12:07:25.190: D/AndroidShowNativePage(1078): before: app init onSuccess
05-08 12:07:25.310: D/AndroidShowNativePage(1078): after: app init onSuccess
05-08 12:07:25.350: D/AndroidShowNativePage(1078): added onPause event handler 
05-08 12:07:25.370: D/AndroidShowNativePage(1078): wlclient init success
05-08 12:26:11.550: D/CordovaActivity(1078): Paused the application!
05-08 12:26:11.550: D/CordovaWebView(1078): Handle the pause
05-08 12:26:11.650: W/PluginManager(1078): THREAD WARNING: exec() call to NativePage.show blocked the main thread for 151ms. Plugin should use CordovaInterface.getThreadPool().
05-08 12:26:12.620: I/Choreographer(1078): Skipped 60 frames!  The application may be doing too much work on its main thread.
05-08 12:26:13.040: D/AndroidShowNativePage(1078): Flush called
05-08 12:26:17.070: I/Choreographer(1078): Skipped 75 frames!  The application may be doing too much work on its main thread.

1 个答案:

答案 0 :(得分:0)

您是否在Eclipse控制台或LogCat视图中出现任何错误?提供LogCat的输出,这应该有所帮助。使用此信息编辑问题。

以下权限也需要添加到AndroidManifest.xml中:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

修改
还有以下教程可以获得信号强度:http://www.firstdroid.com/2010/05/12/get-provider-gsm-signal-strength/

我已经关注它了,它在Android模拟器中对我有用 这是我的Java实现:

package com.testapp;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.widget.Toast;

public class HelloNative extends Activity {

    TelephonyManager        tm;
    MyPhoneStateListener    MyListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        MyListener   = new MyPhoneStateListener();
        tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
        tm.listen(MyListener ,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);

        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        setContentView(linearLayout);

        Button submitButton = new Button(this);
        submitButton.setText("Return to the Web App");
        submitButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                setResult(RESULT_OK);
                finish();
            };
        });

        linearLayout.addView(submitButton);
    }

    private class MyPhoneStateListener extends PhoneStateListener
    {
      /* Get the Signal strength from the provider, each time there is an update */
      @Override
      public void onSignalStrengthsChanged(SignalStrength signalStrength)
      {
         super.onSignalStrengthsChanged(signalStrength);
         Toast.makeText(getApplicationContext(), "Signal strength is: "
            + String.valueOf(signalStrength.getGsmSignalStrength()), Toast.LENGTH_SHORT).show();
      }

    };

}