IBM Worklight - Unable to get network signal strength in Android

前端 未结 1 583
一生所求
一生所求 2021-01-16 07:51

Below is my Java implementation. The idea is to display myUtilString in an edit text in order to see the signal strength in a Hybrid android app while in a WL.N

相关标签:
1条回答
  • 2021-01-16 08:38

    Do you get any errors either in Eclipse console or in the LogCat view? Provide the output from LogCat, that should help. Edit the question with this information.

    The following permission needs to be added to AndroidManifest.xml as well:

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

    Edit:
    There is also the following tutorial to get signal strength: http://www.firstdroid.com/2010/05/12/get-provider-gsm-signal-strength/

    I've followed it, and it worked for me in the Android emulator.
    This is my Java implementation:

    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();
          }
    
        };
    
    }
    
    0 讨论(0)
提交回复
热议问题