为什么我的BroadcastReceiver代码无法执行?

时间:2019-07-12 19:17:21

标签: java android broadcastreceiver

我正在使用BroadcastReceiver来发送短信,但是当手机收到短信时,该功能不起作用。我想在收到短信时做Sth,所以我使用BroadCastReceiver进行了此操作<action android:name="android.provider.Telephony.SMS_RECEIVED"/>,但是当我收到短信时,我的Reciver代码不执行。我为接收方(android:process=":MyPayamakRecProc")设置了一个进程,但是在SMS接收时它不会运行。 这是我的清单的一部分。

<uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <receiver android:name=".MySmsRecieve" android:process=":MyPayamakRecProc">
                <intent-filter android:priority="1234">
                    <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
                </intent-filter>
    </receiver>

我尝试将其与服务结合使用,但没有用。这是我的服务。

public static void ScheduleService() {
        final long REPEAT_TIME = 1000 * 10;
        Log.i("tag","ScheduleService");
        //Thread tmpThread = new Thread(new Runnable() {

        //@Override
        //public void run() {
        AlarmManager service = (AlarmManager) G.currentactivity.getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent(G.currentactivity, MyStartServiceActivity.class);
        PendingIntent pending = PendingIntent.getBroadcast(G.currentactivity, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
        Calendar cal = Calendar.getInstance();
        // start 10 seconds after boot completed
        cal.add(Calendar.SECOND, 10);

        service.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), REPEAT_TIME, pending);
        // }
        //});
        //tmpThread.setPriority(1345);
        //tmpThread.setName("ScheduleService");
        //tmpThread.start();
    }

MyStartServiceActivity

public class MyStartServiceActivity extends BroadcastReceiver {

    @Override
    public void onReceive(final Context context, Intent intent) {
        //Toast.makeText(G.currentactivity, "MyStartServiceReceiver", Toast.LENGTH_SHORT).show();
        Log.i("tag","MyStartServiceActivity");
        Intent service = new Intent(context, SmsService.class);
        context.startService(service);

    }
}

这是SmsService.class的一部分

@Override
    public void onTaskRemoved(Intent rootIntent) {
        //Toast.makeText(getApplicationContext(), "AS onTaskRemoved called", Toast.LENGTH_LONG).show();
        startService(new Intent(this, SmsService.class));
        super.onTaskRemoved(rootIntent);
        ActivitySetting.ScheduleService();
    }

ActivitySetting是我的主要活动。

请帮助我。谢谢

0 个答案:

没有答案