-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
API 26, reworked to remove service and limits
uses an alarm now. Should use a job scheduler, but haven't learn it yet.
- Loading branch information
Showing
9 changed files
with
207 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 34 additions & 35 deletions
69
BroadcastBoot/app/src/main/java/edu/cs4730/broadcastboot/MainActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,71 @@ | ||
package edu.cs4730.broadcastboot; | ||
|
||
import android.app.ActivityManager; | ||
import android.app.ActivityManager.RunningServiceInfo; | ||
import android.app.NotificationChannel; | ||
import android.app.NotificationManager; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
|
||
import android.graphics.Color; | ||
import android.os.Bundle; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.util.Log; | ||
|
||
/* | ||
* This example sets a receiver to a boot message and then makes sure that it's service | ||
* is running on start up. | ||
* This example sets a receiver to a boot message to a receiver. | ||
* the receiver then sets an alarm for X minutes. X is set in the receiver. | ||
* | ||
* services are hard to keep running in the background in API 26, so alarms were used for this | ||
* example. It should be a scheduled job, when I have time to learn ithem. | ||
*/ | ||
|
||
public class MainActivity extends AppCompatActivity { | ||
|
||
public static final String ACTION = "edu.cs4730.broadcastboot.myAction"; | ||
MainFragment mFragment; | ||
final String TAG = "MainActivity"; | ||
public static String id = "test_channel_01"; | ||
|
||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_main); | ||
|
||
String info = "Nothing"; | ||
Bundle extras = getIntent().getExtras(); | ||
if (extras != null) { | ||
info = extras.getString("mText"); | ||
} | ||
Log.wtf(TAG, "info is " + info); | ||
mFragment = MainFragment.newInstance(info); | ||
|
||
if (savedInstanceState == null) { | ||
getSupportFragmentManager().beginTransaction() | ||
.add(R.id.container, mFragment).commit(); | ||
} | ||
//if my service is not running, start it. for testing, before rebooting emulators... so much fun... | ||
if (!isMyServiceRunning(MyService.class)) { | ||
//not running, start it. | ||
Intent i = new Intent(getBaseContext(), MyService.class); | ||
startService(i); | ||
Log.v(TAG, "Started service"); | ||
} | ||
|
||
Log.v(TAG, "started up: " + info); | ||
} | ||
|
||
|
||
private boolean isMyServiceRunning(Class<?> serviceClass) { | ||
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); | ||
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { | ||
if (serviceClass.getName().equals(service.service.getClassName())) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
createchannel(); | ||
} | ||
|
||
@Override | ||
protected void onNewIntent(Intent intent) { | ||
super.onNewIntent(intent); | ||
String info = "Nothing"; | ||
Bundle extras = getIntent().getExtras(); | ||
if (extras != null) { | ||
/* | ||
* for API 26+ create notification channels | ||
*/ | ||
private void createchannel() { | ||
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { | ||
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); | ||
NotificationChannel mChannel = new NotificationChannel(id, | ||
getString(R.string.channel_name), //name of the channel | ||
NotificationManager.IMPORTANCE_DEFAULT); //importance level | ||
//important level: default is is high on the phone. high is urgent on the phone. low is medium, so none is low? | ||
// Configure the notification channel. | ||
mChannel.setDescription(getString(R.string.channel_description)); | ||
mChannel.enableLights(true); | ||
// Sets the notification light color for notifications posted to this channel, if the device supports this feature. | ||
mChannel.setLightColor(Color.RED); | ||
mChannel.enableVibration(true); | ||
mChannel.setShowBadge(true); | ||
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400}); | ||
nm.createNotificationChannel(mChannel); | ||
|
||
info = extras.getString("mText"); | ||
Log.d(TAG, "new value"); | ||
} | ||
Log.v(TAG, "New intent " + info); | ||
|
||
mFragment.settext(info); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 91 additions & 3 deletions
94
BroadcastBoot/app/src/main/java/edu/cs4730/broadcastboot/MyReceiver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,111 @@ | ||
package edu.cs4730.broadcastboot; | ||
|
||
import android.app.AlarmManager; | ||
import android.app.Notification; | ||
import android.app.NotificationManager; | ||
import android.app.PendingIntent; | ||
import android.content.BroadcastReceiver; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.os.Bundle; | ||
import android.support.v4.app.NotificationCompat; | ||
import android.util.Log; | ||
|
||
import java.util.Calendar; | ||
import java.util.Random; | ||
|
||
/* | ||
* this receiver get the on boot completed message then starts the service. | ||
* it's registered in the androidmanifest file. | ||
*/ | ||
public class MyReceiver extends BroadcastReceiver { | ||
|
||
String TAG="MyReceiver"; | ||
|
||
@Override | ||
public void onReceive(Context context, Intent intent) { | ||
int NotiID=1; | ||
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) { | ||
//boot has completed, now time to start our background service. | ||
Log.v("MyReceiver", "Got the boot one!"); | ||
Intent i = new Intent(context, MyService.class); | ||
context.startService(i); | ||
Log.wtf(TAG, "Got the boot one!"); | ||
setalarm(context, 2,NotiID); //NotiID is initialize to 1, which is the start. | ||
} else if (intent.getAction().equals(MainActivity.ACTION)) { //my custom intent | ||
Bundle extras =intent.getExtras(); | ||
if (extras != null) { | ||
NotiID = extras.getInt("notiid"); | ||
} | ||
sendNoti(context, NotiID); | ||
setalarm(context, 2, NotiID+1); //use the next notification ID number. | ||
} | ||
} | ||
|
||
|
||
public void setalarm(Context context, int time, int notiID) { | ||
|
||
//---use the AlarmManager to trigger an alarm--- | ||
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); | ||
//---get current date and time--- | ||
Calendar calendar = Calendar.getInstance(); | ||
//---sets the time for the alarm to trigger in 2 minutes from now--- | ||
calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) + time); | ||
calendar.set(Calendar.SECOND, 0); | ||
|
||
|
||
//---PendingIntent to launch receiver when the alarm triggers- | ||
//Intent notificationIntent = new Intent(getApplicationContext(), MyReceiver.class); | ||
Intent notificationIntent = new Intent(MainActivity.ACTION); | ||
notificationIntent.setPackage("edu.cs4730.broadcastboot"); //in API 26, it must be explicit now. | ||
notificationIntent.putExtra("notiid", notiID); | ||
PendingIntent contentIntent = PendingIntent.getBroadcast(context, notiID, notificationIntent, 0); | ||
Log.i(TAG, "Set alarm, I hope"); | ||
//---sets the alarm to trigger--- | ||
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), contentIntent); | ||
|
||
} | ||
|
||
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, 0); | ||
|
||
//create the notification | ||
Notification notif = new NotificationCompat.Builder(context, MainActivity.id) | ||
.setSmallIcon(R.drawable.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. | ||
} | ||
|
||
} |
Oops, something went wrong.