应用关闭后,服务无法从AlarmManager在Android中启动

时间:2018-07-05 16:04:33

标签: android multithreading service alarmmanager intentservice

任何人都可以请纠正我的方法或建议我正确的方法。我尝试使用广播接收器代替服务,但是这种方法不起作用。即,我使用getBroadcast()代替getService()但是没用。

当未从应用程序窗口关闭应用程序但它关闭时,它正在工作,在未关闭应用程序的情况下,电话不会显示通知并响起它的铃声。

  

MainActivity.class

`public class MainActivity extends AppCompatActivity {

    TimePicker timePicker;
    Button button;
    AlarmManager alarmManager;
    PendingIntent alarmIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        timePicker = findViewById(R.id.timePicker);
        button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar calendar = Calendar.getInstance();
                if (Build.VERSION.SDK_INT >= 23) {
                    calendar.set(
                            calendar.get(Calendar.YEAR),
                            calendar.get(Calendar.MONTH),
                            calendar.get(Calendar.DAY_OF_MONTH),
                            timePicker.getHour(),
                            timePicker.getMinute(),
                            0
                    );
                } else {
                    calendar.set(
                            calendar.get(Calendar.YEAR),
                            calendar.get(Calendar.MONTH),
                            calendar.get(Calendar.DAY_OF_MONTH),
                            timePicker.getCurrentHour(),
                            timePicker.getCurrentMinute(),
                            0
                    );
                }
                Intent intent = new Intent(MainActivity.this,MyIntentService.class);
//This is the intent which will be fired
                alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
                alarmIntent = PendingIntent.getService(getApplicationContext(), 0, intent, 0);
                alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
                Log.e("TAG:","AlarmSet.");
// Log is showing the right statement i.e the alarm was set but it wasnt triggered when the app was closed from the application window
            }
       });

    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.e("TAG","OnPause.");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.e("TAG","OnStop.");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e("TAG","OnDestroy.");
    }
}`
  

IntentService.class

`public class MyIntentService extends IntentService {
 public MyIntentService() {
     super("MyIntentService");
 }

 @Override
 protected void onHandleIntent(@Nullable Intent intent) {
     Log.e("TAG", "Starting service for notification and song");
     Random random = new Random();
     final Notification notification = new  NotificationCompat.Builder(getApplicationContext(), "notify")
            .setContentTitle("New Notification")
            .setContentText("TIme for task.")
            .setSmallIcon(R.mipmap.ic_launcher_round)
            .setPriority(Notification.PRIORITY_MAX)
            .build();
     final NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
     notificationManager.notify(random.nextInt(), notification);
     MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), Settings.System.DEFAULT_RINGTONE_URI);
     mediaPlayer.start();

 }
}`
  

清单文件

`     

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

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service
        android:name=".MyIntentService"
        android:enabled="true"
        android:exported="true" />
 </application>

</manifest>`

0 个答案:

没有答案