Android NotificationManager使手机重新启动
说明: 每次我的应用程序在Android 4.4.4上运行时,用户的手机都会很快重新启动,并且不会向Crashlytics发送崩溃数据 当我说用户的手机重新启动时,这就像是我见过的最快的重新启动 应用程序在以下情况下运行良好:Android NotificationManager使手机重新启动,android,notifications,crash,reboot,Android,Notifications,Crash,Reboot,说明: 每次我的应用程序在Android 4.4.4上运行时,用户的手机都会很快重新启动,并且不会向Crashlytics发送崩溃数据 当我说用户的手机重新启动时,这就像是我见过的最快的重新启动 应用程序在以下情况下运行良好: public void startNotification(){ final Intent hideNotificationIntent = new Intent(MainActivity.this, HideWallpaperNotificationSe
public void startNotification(){
final Intent hideNotificationIntent = new Intent(MainActivity.this, HideWallpaperNotificationService.class);
final Intent stopIntent = new Intent(MainActivity.this, StopWallpaperService.class);
final Intent wallpaperCyclerIntent = new Intent(MainActivity.this, MainActivity.class);
alarmIntent = new Intent(this, SetWallpaperService.class);
alarmIntent.putStringArrayListExtra(Constants.PREF_WALLPAPERS, mWallpapers);
PendingIntent nextWallpaperPI = PendingIntent.getService(this.getApplicationContext(), 300, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent stopWallpaperPI = PendingIntent.getService(this.getApplicationContext(), 300, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent showApp = PendingIntent.getActivity(this.getApplicationContext(), 300, wallpaperCyclerIntent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent hideNotification = PendingIntent.getService(this.getApplicationContext(), 300, hideNotificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//Button
NotificationCompat.Action stopAction = new NotificationCompat.Action.Builder(R.drawable.ic_stop_white_18dp, "Stop", stopWallpaperPI).build();
NotificationCompat.Action nextAction = new NotificationCompat.Action.Builder(R.drawable.ic_arrow_forward_white_24dp, "Next", nextWallpaperPI).build();
NotificationCompat.Action removeAction = new NotificationCompat.Action.Builder(R.drawable.ic_clear_white_18dp, "Hide", hideNotification).build();
Bitmap original;
if(lastSelected == -1)
original = BitmapFactory.decodeFile(mWallpapers.get(0));
else
original = BitmapFactory.decodeFile(mWallpapers.get(lastSelected));
mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_fit)
.setLargeIcon(original)
.setWhen(0)
.setColor(ContextCompat.getColor(this.getApplicationContext(), R.color.colorPrimary))
.setContentTitle("Wallpaper Cycler")
.setContentText("Click to open")
.setContentIntent(showApp)
.addAction(stopAction)
.addAction(nextAction)
.addAction(removeAction);
notificationManager = (NotificationManager) getSystemService(MainActivity.this.NOTIFICATION_SERVICE);
Notification n;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN){
n = mBuilder.build();
}else{
n = mBuilder.getNotification();
}
n.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;
try{
notificationManager.notify(Constants.NOTIFICATION_ID, n);
}catch(Exception e){Log.e(TAG,"Error notifying");}
}
- 4.1.1
- 6.0.1
public void startNotification(){
final Intent hideNotificationIntent = new Intent(MainActivity.this, HideWallpaperNotificationService.class);
final Intent stopIntent = new Intent(MainActivity.this, StopWallpaperService.class);
final Intent wallpaperCyclerIntent = new Intent(MainActivity.this, MainActivity.class);
alarmIntent = new Intent(this, SetWallpaperService.class);
alarmIntent.putStringArrayListExtra(Constants.PREF_WALLPAPERS, mWallpapers);
PendingIntent nextWallpaperPI = PendingIntent.getService(this.getApplicationContext(), 300, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent stopWallpaperPI = PendingIntent.getService(this.getApplicationContext(), 300, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent showApp = PendingIntent.getActivity(this.getApplicationContext(), 300, wallpaperCyclerIntent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent hideNotification = PendingIntent.getService(this.getApplicationContext(), 300, hideNotificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//Button
NotificationCompat.Action stopAction = new NotificationCompat.Action.Builder(R.drawable.ic_stop_white_18dp, "Stop", stopWallpaperPI).build();
NotificationCompat.Action nextAction = new NotificationCompat.Action.Builder(R.drawable.ic_arrow_forward_white_24dp, "Next", nextWallpaperPI).build();
NotificationCompat.Action removeAction = new NotificationCompat.Action.Builder(R.drawable.ic_clear_white_18dp, "Hide", hideNotification).build();
Bitmap original;
if(lastSelected == -1)
original = BitmapFactory.decodeFile(mWallpapers.get(0));
else
original = BitmapFactory.decodeFile(mWallpapers.get(lastSelected));
mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_fit)
.setLargeIcon(original)
.setWhen(0)
.setColor(ContextCompat.getColor(this.getApplicationContext(), R.color.colorPrimary))
.setContentTitle("Wallpaper Cycler")
.setContentText("Click to open")
.setContentIntent(showApp)
.addAction(stopAction)
.addAction(nextAction)
.addAction(removeAction);
notificationManager = (NotificationManager) getSystemService(MainActivity.this.NOTIFICATION_SERVICE);
Notification n;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN){
n = mBuilder.build();
}else{
n = mBuilder.getNotification();
}
n.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;
try{
notificationManager.notify(Constants.NOTIFICATION_ID, n);
}catch(Exception e){Log.e(TAG,"Error notifying");}
}
- 我的朋友在他的4.4.4设备上复制了这个bug
- 我创建的后台服务在手机重新启动后继续(是的,我使用bootReceiver)
public void startNotification(){
final Intent hideNotificationIntent = new Intent(MainActivity.this, HideWallpaperNotificationService.class);
final Intent stopIntent = new Intent(MainActivity.this, StopWallpaperService.class);
final Intent wallpaperCyclerIntent = new Intent(MainActivity.this, MainActivity.class);
alarmIntent = new Intent(this, SetWallpaperService.class);
alarmIntent.putStringArrayListExtra(Constants.PREF_WALLPAPERS, mWallpapers);
PendingIntent nextWallpaperPI = PendingIntent.getService(this.getApplicationContext(), 300, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent stopWallpaperPI = PendingIntent.getService(this.getApplicationContext(), 300, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent showApp = PendingIntent.getActivity(this.getApplicationContext(), 300, wallpaperCyclerIntent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent hideNotification = PendingIntent.getService(this.getApplicationContext(), 300, hideNotificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//Button
NotificationCompat.Action stopAction = new NotificationCompat.Action.Builder(R.drawable.ic_stop_white_18dp, "Stop", stopWallpaperPI).build();
NotificationCompat.Action nextAction = new NotificationCompat.Action.Builder(R.drawable.ic_arrow_forward_white_24dp, "Next", nextWallpaperPI).build();
NotificationCompat.Action removeAction = new NotificationCompat.Action.Builder(R.drawable.ic_clear_white_18dp, "Hide", hideNotification).build();
Bitmap original;
if(lastSelected == -1)
original = BitmapFactory.decodeFile(mWallpapers.get(0));
else
original = BitmapFactory.decodeFile(mWallpapers.get(lastSelected));
mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_fit)
.setLargeIcon(original)
.setWhen(0)
.setColor(ContextCompat.getColor(this.getApplicationContext(), R.color.colorPrimary))
.setContentTitle("Wallpaper Cycler")
.setContentText("Click to open")
.setContentIntent(showApp)
.addAction(stopAction)
.addAction(nextAction)
.addAction(removeAction);
notificationManager = (NotificationManager) getSystemService(MainActivity.this.NOTIFICATION_SERVICE);
Notification n;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN){
n = mBuilder.build();
}else{
n = mBuilder.getNotification();
}
n.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;
try{
notificationManager.notify(Constants.NOTIFICATION_ID, n);
}catch(Exception e){Log.e(TAG,"Error notifying");}
}
问题是位图太大,所以我使用下面的函数来调整它们的大小
Bitmap original;
if(lastSelected == -1)
original = BitmapFactory.decodeFile(mWallpapers.get(0));
else
original = BitmapFactory.decodeFile(mWallpapers.get(lastSelected));
original = returnBitmap(original,150,320);
public Bitmap returnBitmap(Bitmap originalImage, int width, int height){
Bitmap background = Bitmap.createBitmap((int)width, (int)height, Bitmap.Config.ARGB_8888);
float originalWidth = originalImage.getWidth();
float originalHeight = originalImage.getHeight();
Canvas canvas = new Canvas(background);
float scale = width / originalWidth;
float xTranslation = 0.0f;
float yTranslation = (height - originalHeight * scale) / 2.0f;
Matrix transformation = new Matrix();
transformation.postTranslate(xTranslation, yTranslation);
transformation.preScale(scale, scale);
Paint paint = new Paint();
paint.setFilterBitmap(true);
canvas.drawBitmap(originalImage, transformation, paint);
return background;
}