即使应用程序被杀死,我也希望状态栏通知

时间:2014-10-17 04:58:53

标签: android notifications broadcastreceiver android-wifi

我想在WiFi触发时发现状态栏通知,或者即使应用程序被杀死也找到特定的SSID。 我已经完成了代码,但它没有触发通知。

这是我的代码。

    import java.util.List;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.support.v4.app.NotificationCompat;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

public class Myreceiver extends BroadcastReceiver {

    WifiManager wifiManager;
    List<ScanResult> wiScanResults;
    Context context;
    String wifilist[];
    String SSID;


    /**
     * @param args
     */


    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        getnotification();

    }


    public void getnotification()
    {


        ConnectivityManager conManager =(ConnectivityManager)context.getSystemService(context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo= conManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if(networkInfo.isConnected())
        {
            wifiManager= (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            if(wifiInfo!=null&& ! TextUtils.isEmpty(wifiInfo.getSSID()))
            {
                SSID= wifiInfo.getSSID();
            }
        }
        Log.i("SSID", SSID);

        if(!(SSID.equals("xsinfosol")))
        {





        wiScanResults=wifiManager.getScanResults();
        wifilist = new String[wiScanResults.size()];
        for(int i=0;i<wiScanResults.size();i++)
        {
            if(wiScanResults.get(i).SSID.contentEquals("\"xsinfosol\""))
            {
                String networkSSID= wiScanResults.get(i).SSID;
                String networkPASS= "9953007901";


                WifiConfiguration wifiConfiguration = new WifiConfiguration();
                wifiConfiguration.SSID=String.format("\"%s\"", networkSSID);
                wifiConfiguration.preSharedKey=String.format("\"%s\"", networkPASS);

                Toast.makeText(context, "connect to::"+networkSSID, 0).show();

                int netId=wifiManager.addNetwork(wifiConfiguration);
                wifiManager.disconnect();
                wifiManager.enableNetwork(netId,true);
                wifiManager.reconnect();


                wifiConfiguration = new WifiConfiguration();
                wifiConfiguration.SSID="\"\"" + networkSSID + "\"\"";
                wifiConfiguration.preSharedKey="\"\"" + networkPASS + "\"\"";
                wifiManager.addNetwork(wifiConfiguration);



                Intent notificationIntent = new Intent(context,Notify.class);

                notificationIntent.setData(Uri.parse("tel:/"+(int)System.currentTimeMillis()));

                PendingIntent pendingIntent =PendingIntent.getActivity(context, 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
                NotificationManager nManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

                NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
                                        .setWhen(System.currentTimeMillis())
                                        .setContentText("You are connected to Tata Docomo Wifi")
                                        .setContentTitle("Tata Wifi Notifaction")
                                        .setSmallIcon(R.drawable.ic_launcher)
                                        .setTicker("enjoy the Internet")
                                        .setContentIntent(pendingIntent)
                                        .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));


                Notification notification = notificationBuilder.build();

                nManager.notify((int)System.currentTimeMillis(), notification);

            }
        }


    }
    }

}

这是我的Android清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.broadcast"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <receiver android:name="com.example.broadcast.Myreceiver" >
            <intent-filter >
                <action  android:name="android.net.wifi.SCAN_RESULTS"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>

        </receiver>

        <activity
            android:name=".Notify"
            android:label="@string/title_activity_notify" >
        </activity>
    </application>

</manifest>

1 个答案:

答案 0 :(得分:0)

尝试在清单中添加此内容

    <receiver android:name="com.example.broadcast.Myreceiver" >
        <intent-filter >
            <action  android:name="android.net.wifi.SCAN_RESULTS"/>
            <action android:name="android.net.wifi.STATE_CHANGE" />
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>

    </receiver>    

然后检查