Skip to content

Commit

Permalink
replaced deprecated with a worker.
Browse files Browse the repository at this point in the history
  • Loading branch information
JimSeker committed Apr 18, 2022
1 parent b413060 commit afe165f
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 122 deletions.
2 changes: 2 additions & 0 deletions BroadcastBoot2/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
//Work Manager
implementation 'androidx.work:work-runtime:2.7.1'
}
4 changes: 0 additions & 4 deletions BroadcastBoot2/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".MyJobIntentService"
android:exported="true"
android:permission="android.permission.BIND_JOB_SERVICE" />
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import android.graphics.Color;

import androidx.appcompat.app.AppCompatActivity;
import androidx.work.OneTimeWorkRequest;
import androidx.work.WorkManager;

import android.os.Bundle;
import android.view.View;
Expand Down Expand Up @@ -37,13 +39,14 @@ protected void onCreate(Bundle savedInstanceState) {
public void onClick(View view) {
Intent intent = new Intent();
//normally this intent should have something, like data...
MyJobIntentService.enqueueWork(getApplicationContext(), intent);
OneTimeWorkRequest runWork = new OneTimeWorkRequest.Builder(myWorker.class)
.build();
WorkManager.getInstance(getApplicationContext()).enqueue(runWork);
}
});
createchannel();
}


/**
* for API 26+ create notification channels
*/
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import android.content.Intent;
import android.util.Log;

import androidx.work.OneTimeWorkRequest;
import androidx.work.WorkManager;

public class MyReceiver extends BroadcastReceiver {
String TAG = "MyReceiver";

Expand All @@ -13,7 +16,9 @@ public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
//boot has completed, now time to start our background service.
Log.wtf(TAG, "Got the boot one!");
MyJobIntentService.enqueueWork(context, intent); //not sure if should create a new intent or not.
OneTimeWorkRequest runWork = new OneTimeWorkRequest.Builder(myWorker.class)
.build();
WorkManager.getInstance(context).enqueue(runWork);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package edu.cs4730.broadcastboot2;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import java.util.Random;

import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;
import androidx.work.Worker;
import androidx.work.WorkerParameters;

public class myWorker extends Worker {
public myWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
super(context, workerParams);
}

@NonNull
@Override
public Result doWork() {
int NotiID = 1;
boolean done = false;
while (!done) {
sendNoti(getApplicationContext(), NotiID);
Log.wtf("myWorker", "send notification");
try {
Thread.sleep(60000); // 1000 is one second, once a minute would be 60000
} catch (InterruptedException e) {
done = true;
e.printStackTrace();
return Result.failure();
}
}
return Result.success();
}


public void sendNoti(Context context, int notiID) {

String info = "error"; //changed below.
Random myRandom = new Random();
NotificationManager mManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

//---PendingIntent to launch activity if the user selects
// the notification---
Intent notificationIntent = new Intent(context, MainActivity.class);
//random notification
switch (myRandom.nextInt(4)) {
case 0:
info = "Iphone6 is very bendy!";
break;
case 1:
info = "Pixel phones are cool.";
break;
case 2:
info = "Nexus 6 phone is huge at 5.9 inches!";
break;
case 3:
info = "Hope the Samsung 8 note doesn't catch fire!";
break;
default:
info = "No new headline.";
}

notificationIntent.putExtra("mText", info);

PendingIntent contentIntent = PendingIntent.getActivity(context, notiID, notificationIntent, PendingIntent.FLAG_IMMUTABLE);

//create the notification
Notification notif = new NotificationCompat.Builder(context, MainActivity.id)
.setSmallIcon(R.mipmap.ic_launcher)
.setWhen(System.currentTimeMillis()) //When the event occurred, now, since noti are stored by time.
.setContentTitle("New headline!") //Title message top row.
.setContentText(info) //message when looking at the notification, second row
.setContentIntent(contentIntent) //what activity to open.
.setChannelId(MainActivity.id)
.setAutoCancel(true) //allow auto cancel when pressed.
.build(); //finally build and return a Notification.
//Show the notification
mManager.notify(1, notif); //and if we want different notifications, use notiID here instead of 1.
}



}

0 comments on commit afe165f

Please sign in to comment.