如果应用程序位于android的后台,如何发送通知?
目前我正在进行蓝牙扫描,这意味着我将继续扫描设备,如果附近有设备,我将在屏幕上显示,但是,如果应用程序位于后台,我需要向用户显示通知 现在我的工作逻辑是这样的,有一个主类启动扫描服务,扫描服务的任务是返回扫描设备的列表。主要的任务是显示/处理结果 代码如下所示: 服务如果应用程序位于android的后台,如何发送通知?,android,service,notifications,android-activity,android-lifecycle,Android,Service,Notifications,Android Activity,Android Lifecycle,目前我正在进行蓝牙扫描,这意味着我将继续扫描设备,如果附近有设备,我将在屏幕上显示,但是,如果应用程序位于后台,我需要向用户显示通知 现在我的工作逻辑是这样的,有一个主类启动扫描服务,扫描服务的任务是返回扫描设备的列表。主要的任务是显示/处理结果 代码如下所示: 服务 public class Detector extends Service implements IBeaconConsumer { protected static final String TAG = "Rangin
public class Detector extends Service implements IBeaconConsumer {
protected static final String TAG = "RangingActivity";
private IBeaconManager iBeaconManager;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
iBeaconManager = IBeaconManager.getInstanceForApplication(this);
iBeaconManager.bind(this);
Log.d("test1","bind");
return super.onStartCommand(intent, flags, startId);
}
}
Main
private class DataUpdateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("scanResult")) {
ArrayList<IBeacon> beacons = (ArrayList<IBeacon>) intent.getSerializableExtra("beacons");
for(IBeacon be : beacons){
if (be.getProximityUuid().equals("ebefd083-70a2-47c8-9837-e7b5634df524")) {
if (be.getMajor() == 2) {
a_rssi = be.getRssi();
} else if (be.getMajor() == 1) {
b_rssi = be.getRssi();
}
}
}
if(a_rssi > b_rssi) {
Log.d("test1","at shop");
//at shop,need case handling if more than 1 shop beacon
showAd(R.drawable.offer1);
if (timer != null)
timer.cancel();
timer = null;
timeCount = 0;
Intent msg = new Intent("timerUpdate");
msg.putExtra("timeCount", timeCount);
sendBroadcast(msg);
} else {
Log.d("test1","at park");
//at car park
if (timer == null) {
timer = new Timer(true);
timer.schedule(new MyTimer(), 1000, 1000);
}
}
}
}
}
私有类DataUpdateReceiver扩展了BroadcastReceiver{
@凌驾
公共void onReceive(上下文、意图){
if(intent.getAction().equals(“scanResult”)){
ArrayList beacons=(ArrayList)intent.getSerializableExtra(“beacons”);
用于(IBeacon be:信标){
如果(be.getProximityUID()等于(“ebefd083-70a2-47c8-9837-e7b5634df524”)){
if(be.getMajor()==2){
a_rssi=be.getRssi();
}else if(be.getMajor()==1){
b_rssi=be.getRssi();
}
}
}
如果(a_rssi>b_rssi){
日志d(“测试1”,“在车间”);
//在车间,如果超过1个车间,则需要案例处理
showAd(R.可提取报价1);
如果(计时器!=null)
timer.cancel();
定时器=空;
时间计数=0;
意向消息=新意向(“时间更新”);
msg.putExtra(“timeCount”,timeCount);
发送广播(msg);
}否则{
日志d(“测试1”,“在公园”);
//在停车场
如果(计时器==null){
定时器=新定时器(真);
timer.schedule(新的MyTimer(),10001000);
}
}
}
}
}
问题是,如果需要添加通知,是否需要将流程部件代码(位于主类中)移动到服务?我怎样才能做到呢?谢谢您可以显示来自任何主类或服务类的通知。如果要显示来自Main的通知,请在onReceive(上下文,Intent-Intent)方法(COntext.getSystemService())中使用上下文 请尝试以下代码段:
NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,
"Hello from service", System.currentTimeMillis());
Intent intent = new Intent(this, MainActivity.class);
notification.setLatestEventInfo(this, "contentTitle", "contentText",
PendingIntent.getActivity(this, 1, intent, 0));
manager.notify(123, notification);
首先,您需要检查您的应用程序是否在后台。 您可以在应用程序中的每个活动上调用以下代码:
/**
* Checks if the application is being sent in the background (i.e behind
* another application's Activity).
*
* @param context the context
* @return <code>true</code> if another application will be above this one.
*/
public static boolean isApplicationSentToBackground(final Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasks = am.getRunningTasks(1);
if (!tasks.isEmpty()) {
ComponentName topActivity = tasks.get(0).topActivity;
if (!topActivity.getPackageName().equals(context.getPackageName())) {
return true;
}
}
return false;
}
在清单文件中添加此行:
<uses-permission android:name="android.permission.GET_TASKS" />
private void addNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
String appname = context.getResources().getString(R.string.app_name);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification;
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, myactivity.class), 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(
context);
notification = builder.setContentIntent(contentIntent)
.setSmallIcon(icon).setTicker(appname).setWhen(0)
.setAutoCancel(true).setContentTitle(appname)
.setContentText(message).build();
notificationManager.notify(0 , notification);
}