【发布时间】:2018-09-10 10:14:39
【问题描述】:
我想在从相机应用拍摄新照片时从我的应用创建通知。我想在我的应用程序未运行时实现这一点。我正在使用广播接收器来执行此操作。 这是我的代码...
在 Android 清单中..
<receiver
android:name=".receivers.CameraEventReceiver"
android:label="CameraEventReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.hardware.action.NEW_PICTURE" />
<data android:mimeType="image/*" />
</intent-filter>
</receiver>
在我的接收器类中
public class CameraEventReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel channel = new NotificationChannel("CHANNEL_ID", "my channel name", NotificationManager.IMPORTANCE_HIGH);
manager.createNotificationChannel(channel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "CHANNEL_ID")
.setColor(ContextCompat.getColor(context, R.color.colorPrimary))
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Picture Taken")
.setContentText("here is the uri : "+intent.getData())
.setDefaults(NotificationCompat.DEFAULT_VIBRATE)
.setAutoCancel(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN && Build.VERSION.SDK_INT < Build.VERSION_CODES.O){
builder.setPriority(NotificationCompat.PRIORITY_HIGH);
}
manager.notify(222, builder.build());
}
}
当应用程序运行时,它适用于 Android 6.0... 但它不适用于较新的版本。 我能做些什么来实现这一目标? 我希望它支持所有 android 版本高于 4.1 (JELLY_BEAN) 的设备
提前致谢
【问题讨论】:
-
尝试使用最新版本android的通知渠道,参考:stackoverflow.com/questions/49590489/…
标签: android android-notifications intentfilter android-camera-intent android-broadcastreceiver