(Android)在服务中消失通知

时间:2016-11-24 06:06:24

标签: android service notifications

我的申请导致了问题。

当makePush()方法实现时,它会发出通知。但是,通知立即消失。我必须看到这个通知。然后,我必须能够删除它。一旦通知到达,我就无法阅读。

如何解决这个问题?

这是我的代码:

Service.class

import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.media.RingtoneManager;
import android.net.Uri;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v7.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import wowo.wowo_client.R;
import wowo.wowo_client.Utillity.Rest.ServerRequest;
import wowo.wowo_client.Utillity.wowo_client;


public class Background extends Service implements Runnable {
    List<NameValuePair> params;
    NotificationCompat.Builder builder;
    private wowo_client wc;

    private ArrayList<String> bssidList = new ArrayList<>();
    private WifiManager wifiManager;

    private Handler mHandler;
    private final int TIMER_PERIOD = 7000;

    private boolean pushCount = false;
    private String pushTitle;
    private String pushBody;

    @Override
    public void onCreate() {
        super.onCreate();
        mHandler = new Handler();
        wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        wc = (wowo_client) getApplicationContext();

        mHandler.postDelayed(this, TIMER_PERIOD);
        return START_REDELIVER_INTENT;
    }

    @Override
    public void run() {
        if (wowo_client.getWifiBssidList().size() == 0) {
            wowo_client.setServiceStatus(false);
            stopSelf();
        } else {
            scanWifi();
        }
    }

    private void scanWifi() {
        Log.d("scanWIfi", "fragment_start");
        List<ScanResult> scanResult = wifiManager.getScanResults();
        bssidList.clear();

        String bssid;
        for (ScanResult result : scanResult) {
            if (result.SSID.length() != 0) {
                bssid = result.BSSID;
                bssidList.add(bssid);
            }
        }
        scanResult.clear();
        compareBssid();
    }

    private void compareBssid() {
        for (int index1 = 0; index1 < wowo_client.getWifiBssidList().size(); index1++) {
            for (int index2 = 0; index2 < bssidList.size(); index2++) {
                if (wowo_client.getWifiBssid(index1).equals(bssidList.get(index2)))
                    getPush(index1);
            }
        }
    }


    private void getPush(int index) {

        params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("bssid", wowo_client.getBssid1(index)));

        ServerRequest sr = new ServerRequest();
        JSONObject json = sr.getJSON("http://59.26.68.181:3001/client", params);

        if (json != null) {
            try {
                pushTitle = json.getString("push_title");
                pushBody = json.getString("push_body");

                if (!pushCount) {
                    makePush();
                    pushCount = true;
                }

            } catch (Exception e) {
            }
        }
    }


    private void makePush() {
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        builder = new NotificationCompat.Builder(getApplicationContext());

        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setAutoCancel(true);
        builder.setWhen(System.currentTimeMillis());
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
        builder.setSound(defaultSoundUri);
        builder.setContentTitle(pushTitle);
        builder.setContentText(pushBody);

        Notification n = builder.getNotification();

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(0, n);
    }


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

0 个答案:

没有答案