如果强行停止,我正在尝试重启我的应用程序接收短信。这是我的代码。 它没有重新启动app.Should我会尝试将接收器写为另一个类。 在清单中:
<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.
答案 0 :(得分:0)
您在清单中声明BroadcastReceiver
- 即静态接收器,但事实上您没有这样的课程,而且您正在活动中创建动态接收器。
您实际所做的是在活动开始时注册您的接收器,但您想要反过来(一旦接收器接收到广播,就开始您的活动/应用)。
您需要创建一个名为SMSbr extends BroadcastReceiver
的类,您可以在那里执行逻辑。
这样,您将始终注册接收器,并且当收到SMS广播时,它将唤醒您的应用程序。