2014-02-14 33 views
1

我一直在使用廣播接收器開發一個沒有UI的簡單應用程序。沒有GUI的Android應用程序

該應用程序不包含任何活動。

我給了必要的權限。

我把代碼從這個網址:http://developerandro.blogspot.in/2013/09/check-internet-connection-using.html

該應用程序顯示舉杯「未連接到互聯網」當我點擊改變WiFi狀態。它工作正常。

但我的問題是有一個活動註冊在我的清單文件中,我沒有。所以我從清單中刪除這些行。然後不顯示吐司,我也檢查日誌。沒有輸出更改wifi狀態。

爲什麼發生這種情況?請幫我傢伙...

這裏是清單文件:

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

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

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

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.broadcast_internetcheck.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <receiver 
      android:name="com.example.broadcast_internetcheck.NetworkChangeReceiver" 
      android:label="NetworkChangeReceiver" > 
      <intent-filter> 
       <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> 
       <action android:name="android.net.wifi.WIFI_STATE_CHANGED" /> 
      </intent-filter> 
     </receiver> 
    </application> 

</manifest> 

這裏是我的廣播接收器類:

public class NetworkChangeReceiver extends BroadcastReceiver{ 

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

     String status = NetworkUtil.getConnectivityStatusString(context); 
      /*Above line will return the status of wifi */ 
     Toast.makeText(context, status, Toast.LENGTH_LONG).show(); 

    } 
} 
+0

你的意思是你無法連接到互聯網?是這樣嗎? –

+0

這不是我的情況,當我改變我的wifi狀態時,我需要一個簡單的敬酒。 –

+0

所以你想做吐司按鈕,我不是你的代碼,但我會爲你做一個簡單的吐司按鈕? –

回答

7
+0

不錯的工作,我想。 –

0

如果你不希望任何活動檢查做這個答案。其實你必須爲此創建服務:link

+0

是的,我知道服務。但不應該有任何活動。 –

1

你可以使用service來代替。但顯示Toast通過service位,而不是複雜的,你可以爲無Internet連接顯示notification通過service

+0

你能告訴我如何使用服務通知嗎?那簡直太好了。 –

+0

http://stackoverflow.com/questions/21603884/android-notify-from-my-alarm-class/21604104#21604104 – ryderz8

0

創建一個透明的活動。在活動處於活動狀態時啓動敬酒並立即完成活動。