2013-02-26 37 views
5

如何在用戶進入帶有Wi-Fi的區域時打開我的應用程序?這可能嗎?假設我的應用程序是onPause()狀態(意味着我的設備的主屏幕)。現在當設備與WiFi連接。它會自動打開我的應用程序。與WiFi連接時打開應用程序

回答

5

嘗試添加廣播接收器並在無線連接開始您的活動時監聽網絡更改。這樣的解決方案

public class ConnectivityReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 

       ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
       NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 
       NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 
       if (((null != wifi)&&(wifi.isAvailable())) || ((null != mobile)&&(mobile.isAvailable()))){ 
        Intent uplIntent = new Intent(context, YourActivity.class); 
        uplIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
        context.startActivity(uplIntent); 
       } 

    } 
} 

而添加的東西來體現

<receiver android:name=".receiver.ConnectivityReceiver"> 
     <intent-filter> 
      <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> 
     </intent-filter> 
    </receiver> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
+0

我得到了錯誤,當我禁用WiFi 。 java.lang.RuntimeException:無法實例化接收器com.example.text.ConnectivityReceiver:java.lang.ClassNotFoundException:com.example.text.ConnectivityReceiver在加載器中dalvik.system.PathClassLoader [/data/app/com.example。 test-2.apk] – 2013-02-27 06:45:40

+0

>當我的應用程序onPause()狀態(意味着設備的主屏幕)。當設備與WiFi連接時,它會自動打開我的應用程序。 – 2013-02-27 12:13:38

+0

@ prozhyga->當我的應用程序onPause()狀態(意味着設備的主屏幕)。當設備與WiFi連接時,它會自動打開我的應用程序。 – 2013-02-28 07:51:49

0

我可以想像的是onPause()法這樣的改寫:

@Override 
public void onPause() { 
    String conn_context = Context.WIFI_SERVICE; 
    final WifiManager wifi = (WifiManager) getSystemService(conn_context); 
    if (wifi.isWifiEnabled()) 
    { 
     super.onResume(); 
    } 
    else 
    { 
     super.onPause(); 
    } 
} 

但你也必須想法子來處理真正的onPause事件。

+0

@ goody->當我的應用程序onPause ()狀態(表示設備的主屏幕)。當設備與WiFi連接時,它會自動打開我的應用程序。 – 2013-02-28 07:51:27

相關問題