应用程序 GetGSMStrength 意外关闭。请重试

发布于 2024-12-29 08:11:32 字数 2350 浏览 1 评论 0原文

我是 android 的新手..我创建了以下应用程序来返回信号强度,

package com.example.GetGsmSignalStrength;

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;
import android.os.Bundle;

public class GetGsmSignalStrength extends Activity
{
   /* This variables need to be global, so we can used them onResume and onPause method to
      stop the listener */
   TelephonyManager        Tel;
   MyPhoneStateListener    MyListener;

   /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        /* Update the listener, and start it */
        MyListener   = new MyPhoneStateListener();
        Tel       = ( TelephonyManager )getSystemService(Context.TELEPHONY_SERVICE);
      Tel.listen(MyListener ,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
    }

    /* Called when the application is minimized */
    @Override
   protected void onPause()
    {
      super.onPause();
      Tel.listen(MyListener, PhoneStateListener.LISTEN_NONE);
   }

    /* Called when the application resumes */
   @Override
   protected void onResume()
   {
      super.onResume();
      Tel.listen(MyListener,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
   }

   /* —————————– */
    /* Start the PhoneState listener */
   /* —————————– */
    private class MyPhoneStateListener extends PhoneStateListener
    {
      /* Get the Signal strength from the provider, each tiome there is an update */
      @Override
      public void onSignalStrengthsChanged(SignalStrength signalStrength)
      {
         super.onSignalStrengthsChanged(signalStrength);
         Toast.makeText(getApplicationContext(), "Go to Firstdroid!!! GSM Cinr = "
            + String.valueOf(signalStrength.getGsmSignalStrength()), Toast.LENGTH_SHORT).show();
      }

    };/* End of private Class */

}/* GetGsmSignalStrength */

但是当我运行它时,它会在弹出框中显示标题中提到的错误。我已经在 androidmanifest.xml 中进行了必要的权限更改,这就是我所做的全部。

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

为什么会出现错误?一般什么时候出现? 谢谢

I am a newbie to android..I have created the following application to return the Signal Strength

package com.example.GetGsmSignalStrength;

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;
import android.os.Bundle;

public class GetGsmSignalStrength extends Activity
{
   /* This variables need to be global, so we can used them onResume and onPause method to
      stop the listener */
   TelephonyManager        Tel;
   MyPhoneStateListener    MyListener;

   /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        /* Update the listener, and start it */
        MyListener   = new MyPhoneStateListener();
        Tel       = ( TelephonyManager )getSystemService(Context.TELEPHONY_SERVICE);
      Tel.listen(MyListener ,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
    }

    /* Called when the application is minimized */
    @Override
   protected void onPause()
    {
      super.onPause();
      Tel.listen(MyListener, PhoneStateListener.LISTEN_NONE);
   }

    /* Called when the application resumes */
   @Override
   protected void onResume()
   {
      super.onResume();
      Tel.listen(MyListener,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
   }

   /* —————————– */
    /* Start the PhoneState listener */
   /* —————————– */
    private class MyPhoneStateListener extends PhoneStateListener
    {
      /* Get the Signal strength from the provider, each tiome there is an update */
      @Override
      public void onSignalStrengthsChanged(SignalStrength signalStrength)
      {
         super.onSignalStrengthsChanged(signalStrength);
         Toast.makeText(getApplicationContext(), "Go to Firstdroid!!! GSM Cinr = "
            + String.valueOf(signalStrength.getGsmSignalStrength()), Toast.LENGTH_SHORT).show();
      }

    };/* End of private Class */

}/* GetGsmSignalStrength */

but when I run it its showing the error mentioned in the title in a pop up box. I have made the necessary permission changes in androidmanifest.xml and thats all what I have done.

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

Why is the error showing up? When does it shows up in general?
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初与友歌 2025-01-05 08:11:32

每当应用程序导致故障(异常)时,“错误”就会出现,而该应用程序的设计并未适当处理此类故障。

在 Java 中,异常通常与发生的错误类型、详细说明错误的消息以及允许准确定位代码中发生错误的位置的堆栈跟踪相关联。

当应用程序未处理异常时,应用程序将被强制关闭并且异常将被记录到 Android 日志文件“logcat”中,可通过 DDMS 或通过 从 shell 访问adb logcat

The "error" shows up every time an application causes a failure, an exception, which it was not designed to handle appropriately.

In Java, an exception is usually associated with the type of error that occurred, a message detailing the error, and a stack trace which allows to locate where exactly in the code the error occurred.

When an exception is not handled by the application the App is forced to close and the exception will be logged to the Android log file, the 'logcat', accessible via DDMS or from the shell via adb logcat.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文