0

현재 블루투스 스캔을하고 있습니다. 즉, 기기를 계속 스캔 할 예정이며 주변에 기기가있는 경우 화면에 표시합니다. 앱이 백그라운드에있는 경우 사용자에게 알림을 표시해야합니다. .앱이 백그라운드에서 Android에있는 경우 알림을 보내려면 어떻게해야하나요?

그리고 내 작업 논리는 다음과 같습니다. 검사 서비스를 시작하는 기본 클래스가 있습니다. 검사 서비스 작업은 검사 된 장치 목록을 반환하는 것입니다. 그리고 주 수업 직업은 결과를 표시/처리하는 것입니다. 내가 알림을 추가해야하는 경우

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); 
       } 
      } 

     } 
    } 
} 

문제는, 내가 이동해야 않습니다되어

서비스 :

코드는 다음과 같다 프로세스 파트 코드 (주 클래스에 있음)가 서비스를 제공합니까? 그것을 어떻게 성취 할 수 있습니까? 감사합니다.

답변

1

먼저 당신이 필요로 : 당신은 홈페이지에서 알림을 표시 할 경우

하는이 코드를 시도 onReceive (컨텍스트 컨텍스트, 의도 의도) 방법 (context.getSystemService())에서 컨텍스트를 사용 신청서가 백그라운드에 있는지 확인하십시오. 당신은 응용 프로그램에서 모든 활동에 onPause()에 코드 아래에 호출 할 수 있습니다

/** 
* 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; 
} 

이 매니페스트 파일에이 줄을 추가

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); 

} 
: 알림을 추가하려면

<uses-permission android:name="android.permission.GET_TASKS" /> 

이 코드를 추가 할 수 있습니다

1

메인 또는 서비스 클래스의 알림을 표시 할 수 있습니다.

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); 
관련 문제