为什么我的AlarmManager不会触发BroadcastReceiver

时间:2016-07-14 09:40:06

标签: java android android-studio alarmmanager

在我的应用程序中,我尝试使用BroadcastReceiver呼叫AlarmManager每分钟拨打一次服务。但由于某种原因,它没有被调用。我无法弄清楚这个的原因。我正在通过一个有效的项目来编写这段代码,但我找不到任何不同的东西。请帮忙。

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.shan.chathuranga.smsscheduler">

    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_CONTACTS" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".activities.SplashScreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".activities.MainActivity"/>
        <activity android:name=".activities.ServiceChecker"/>

        <service
            android:name="services.MessageSendingService"
            android:enabled="true"
            android:exported="true"/>

        <receiver android:name="broadcastReceiver.MessageServiceScheduler">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <receiver android:name="broadcastReceiver.MessageServiceTrigger"/>

        <receiver
            android:name="broadcastReceiver.SendBroadcastReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="SMS_SENT" />
            </intent-filter>
        </receiver>

        <receiver
            android:name="broadcastReceiver.DeliveryBroadcastReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="SMS_DELIVERED" />
            </intent-filter>
        </receiver>

    </application>

</manifest>

SplashScreen.java

public class SplashScreen extends AppCompatActivity {

    private Button mainUI;
    private TextView dateTime;
    private Button serviceChecker;
    private static final long REPEAT_TIME = 1000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_screen);

        this.initializeDatabase();
        this.triggerService();

        mainUI = (Button) findViewById(R.id.main_ui);
        dateTime = (TextView) findViewById(R.id.date_time);
        serviceChecker = (Button) findViewById(R.id.service);

        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Calendar cal = Calendar.getInstance();
        dateTime.setText(dateFormat.format(cal.getTime()));

        mainUI.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(SplashScreen.this, MainActivity.class);
                startActivity(i);
            }
        });

        serviceChecker.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(SplashScreen.this, ServiceChecker.class);
                startActivity(i);
            }
        });

    }

    public void initializeDatabase() {
        DBAdapter dbAdapter = new DBAdapter(this);
        dbAdapter.open();
        dbAdapter.close();
    }

    public void triggerService() {

        Intent intent = new Intent(this, MessageServiceTrigger.class);
        PendingIntent pending = PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Calendar cal = Calendar.getInstance();
        alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), REPEAT_TIME, pending);

    }
}

MessageServiceTrigger.java

public class MessageServiceTrigger extends BroadcastReceiver {

    private static final String TAG = MessageServiceTrigger.class.getSimpleName();

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG,TAG+" onReceive get called");
    }
}

3 个答案:

答案 0 :(得分:1)

首先,您需要更改REPEAT_TIME = 1000;这相当于1秒,而不是一分钟。

private static final long REPEAT_TIME = 60000;

接下来尝试更改您的triggerService()方法,如下所示。

public void triggerService() {
    Intent intent = new Intent(this, MessageServiceTrigger.class);
    PendingIntent pending = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), REPEAT_TIME, pending);
}

答案 1 :(得分:0)

试试这个

POST mystuff/_update_by_query
{
  "script": {
    "inline": "ctx._source.recency = (DateTime.now().getMillis() - DateTime.parse(ctx._source.maxdate).getMillis())/(24*60*60*1000)"
  },
  "query": {
    "match_all": {
    }
  }
}

答案 2 :(得分:0)

是否有任何规则规定您不应使用大写字母表示包名。因为那是我一整天都在浪费的东西。由于我的项目中有另外四个广播接收器,因此我创建了一个名为broadcastReceiver的包。那是我放置MessageServiceTrigger课程的地方。我将其更改为broadcast_receiver,因为我的代码中没有任何其他内容可以更改,而且我与我合作的项目具有相同的代码,工作正常。现在它正在运作。谢谢你的所有答案。