如何,如果应用程序是在Android的后台发送通知? [英] How to send notification if the app is at the background in android?
问题描述
目前我工作的蓝牙扫描,这意味着我将继续扫描装置,如果附近的设备,我将显示它在屏幕上,但是,如果应用程序是在后台,我需要显示通知用户。
Currently I am working on bluetooth scan, that means I will keep scanning the device, if there is device nearby , I will show it on screen, however, if the app is at the background, I need to show notification to the user.
现在我的工作逻辑是这样的,有引起火灾的扫描服务主类,扫描服务工作是返回扫描的设备列表。而主类的工作是显示/进程的结果。
And now my working logic is like this, there is a main class that fire the scan service, the scan service job is to return a list of scanned device. And the main class job is to display/ process the result.
在code是这样的:
服务
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);
}
}
主要
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);
}
}
}
}
}
现在的问题是,如果我需要补充通知,就我需要推动这一进程的一部分code(这是在主类)的服务?我怎样才能实现呢?谢谢
The problem is , if I need to add notification, does I need to move the process part code (which is in the main class) to service? How can I achieve it? Thanks
推荐答案
首先,你需要检查,如果您的应用程序在后台。
您可以拨打以下code对的onPause()
在应用程序中的每一次活动:
First you need to check if your application is in background.
You can call below code on onPause()
on every activity in your application:
/**
* 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;
}
添加此行中的清单文件:
Add this line in your manifest file :
<uses-permission android:name="android.permission.GET_TASKS" />
有关补充通知中,您可以添加此code:
For adding notification you can add this code :
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);
}
这篇关于如何,如果应用程序是在Android的后台发送通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!