0

如果强制停止,我正在尝试在收到短信时重新启动我的应用程序。这是我的代码。它没有重新启动应用程序。我是否应该尝试将接收器写为另一个类。在清单中:

   <uses-permission android:name="android.permission.SEND_SMS" />
   <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <receiver android:name=".MySMSbr"> 
     <intent-filter> 
        <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
     </intent-filter> 
    </receiver> 

 My mainActivity onCreate() :

   private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
BroadcastReceiver SMSbr;

  public void onCreate(Bundle savedInstanceState) {
    Toast.makeText(getApplicationContext(),"in OnCreate", Toast.LENGTH_LONG).show();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);      
     SMSbr = new BroadcastReceiver() 
        {

          @Override
          public void onReceive(Context context,Intent intent) 
          {
              this.abortBroadcast();
              Toast.makeText(context, "in onReceive", Toast.LENGTH_LONG).show();
              toggleLogging(AppSettings.getServiceRunning(MainActivity.this),
                        AppSettings.getLoggingInterval(MainActivity.this));
              this.clearAbortBroadcast();
            }//end of onReceive method

          };//end of BroadcastReceiver

          IntentFilter SMSfilter = new IntentFilter(SMS_RECEIVED);
          this.registerReceiver(SMSbr, SMSfilter);
  }
    in togglelogging the service is started
    where is it going wrong.
4

1 回答 1

0

您在清单中声明 a BroadcastReceiver- 即静态接收器,但实际上您没有这样的类,并且您正在活动中创建动态接收器。

您实际上正在做的是在您的活动开始时注册您的接收器,但您想要相反的方式(一旦接收器接收到广播就启动您的活动/应用程序)。
您需要创建一个被调用的类,在SMSbr extends BroadcastReceiver那里您可以执行您的逻辑。

这样,您将始终注册接收器,并且当接收到 SMS 广播时,它将唤醒您的应用程序。

于 2013-04-08T12:15:14.860 回答