应用关闭时,服务和接收器不起作用

时间:2018-03-27 14:20:10

标签: android service process alarmmanager receiver

我正在一个项目中安排一个日历任务并在特定的时间内完成工作,孔代码没问题,正常工作,但是如果我在特定时间强制关闭我的应用程序我在应用程序开始不做我的任务它给我一个“不幸的TransferirLigações已停止”。 我认为计时器和AlarmManager正在工作,但不执行我的任务,给出错误。

我尝试了各种Scheduling任务来执行此操作,AlarmManager和Receiver,Service,IntentService,Job Scheduler和一些插件来执行此操作,但没有任何工作,都让我“不幸的是TransferirLigações已停止”。

任何人都可以帮助我吗?我认为这是Manifest或Permisions中的一些错误,但我不知道。

代码:

MainActivity,startAlarm类:

 private void startAlarm() {
            Intent intent = new Intent(getApplicationContext(), AlarmToastReceiver.class);
            final PendingIntent pIntent = PendingIntent.getBroadcast(this, AlarmToastReceiver.REQUEST_CODE,
                    intent, PendingIntent.FLAG_UPDATE_CURRENT);
            AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
            alarm.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pIntent);

        }

接收器类:     package com.example.usuario.tranferircalls;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;


public class AlarmToastReceiver extends BroadcastReceiver {
    public static final int REQUEST_CODE = 12345;


    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent(context, MyTestService.class);
        i.putExtra("foo", "bar");
        context.startService(i);
    }
}

IntentService:

package com.example.usuario.tranferircalls;

import android.app.IntentService;

import android.content.Intent;

import java.util.concurrent.TimeUnit;

import static com.example.usuario.tranferircalls.MainActivity.cancelAlarm;
import static com.example.usuario.tranferircalls.MainActivity.retornarChamada;


public class MyTestService extends IntentService {
    public MyTestService() {
        // Used to name the worker thread
        // Important only for debugging
        super(MyTestService.class.getName());
    }


    @Override
    protected void onHandleIntent(Intent intent) {
        retornarChamada(0); //My Task, that i want to do when certain time pass.
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        retornarChamada(1);
        cancelAlarm();
    }
}

MyManifest:

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

    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:installLocation="internalOnly"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver
            android:name=".AlarmToastReceiver"
            android:exported="true"
            android:process=":remote"
            android:enabled="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"></action>
            </intent-filter>
        </receiver>
        <service
            android:name=".MyTestService"
            android:enabled="true"
            android:exported="false"/>
    </application>
</manifest>

基本上,那是我的应用程序,它有90%的工作,但我想关闭应用程序并且它有效,因为我确定我的工作/任务能够正常运行。

非常感激,Noninus。

3 个答案:

答案 0 :(得分:0)

试试这个:

AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
int ALARM_TYPE = AlarmManager.RTC_WAKEUP;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
    am.setExactAndAllowWhileIdle(ALARM_TYPE, calendar.getTimeInMillis(), pendingIntent);
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
    am.setExact(ALARM_TYPE, calendar.getTimeInMillis(), pendingIntent);
else
    am.set(ALARM_TYPE, calendar.getTimeInMillis(), pendingIntent);

答案 1 :(得分:0)

用于计算时间的代码,它是一个TimePicker,它获取时间,在静态Calendar cal中设置,然后用它来启动AlarmManager!

Calendar mcurrentTime;
                    mcurrentTime = Calendar.getInstance();
                    mcurrentTime.setTimeInMillis(System.currentTimeMillis());


                    int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
                    int minute = mcurrentTime.get(Calendar.MINUTE);

                    TimePickerDialog mTimePicker;
                    mTimePicker = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {


                        @Override
                        public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {           
                            cal.set(Calendar.HOUR_OF_DAY,selectedHour);
                            cal.set(Calendar.MINUTE,selectedMinute);
                            endTime = cal.getTime();
                        }
                    }, hour, minute, true);//Yes 24 hour time

                    mTimePicker.setTitle("Escolha a Hora para as chamadas voltarem!");
                    mTimePicker.show();

答案 2 :(得分:0)

我会尝试:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;


public class AlarmToastReceiver extends BroadcastReceiver {
    public static final int REQUEST_CODE = 12345;


    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent(context, MyTestService.class);
        i.putExtra("foo", "bar");
        context.startService(i);
    }
}