2016-04-04 17 views
-3

アクティビティのボタンではなく、内蔵のWi-Fiをオンにすると、トーストメッセージを表示します。私の目標は、ランチャーを作成せずにバックグラウンドで作業するアプリケーションを作成することです。私は試しましたが、まだトーストはありません。どのような活動なしでAndroidでBraodCastReceiverを起動するには?

public class Receiver extends BroadcastReceiver { 

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

     String status = NetworkUtil.getConnectivityStatusString(context); 

     Toast.makeText(context, status, Toast.LENGTH_LONG).show(); 
    } 
} 

と私のNetworkUtilクラスはここにある:私のレシーバクラスは、ここにある

public class NetworkUtil { 

    public static int TYPE_WIFI = 1; 
    public static int TYPE_MOBILE = 2; 
    public static int TYPE_NOT_CONNECTED = 0; 


    public static int getConnectivityStatus(Context context) { 
     ConnectivityManager cm = (ConnectivityManager) context 
       .getSystemService(Context.CONNECTIVITY_SERVICE); 

     NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); 
     if (null != activeNetwork) { 
      if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) 
       return TYPE_WIFI; 

      if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) 
       return TYPE_MOBILE; 
     } 
     return TYPE_NOT_CONNECTED; 
    } 

    public static String getConnectivityStatusString(Context context) { 
     int conn = NetworkUtil.getConnectivityStatus(context); 
     String status = null; 
     if (conn == NetworkUtil.TYPE_WIFI) { 
      status = "Wifi enabled"; 
     } else if (conn == NetworkUtil.TYPE_MOBILE) { 
      status = "Mobile data enabled"; 
     } else if (conn == NetworkUtil.TYPE_NOT_CONNECTED) { 
      status = "Not connected to Internet"; 
     } 
     return status; 
    } 

} 

と私のManifest.xmlはここのようなものです:私はここにいる作っていますどのような間違い

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="net.xyz.network" 
android:versionCode="1" 
android:versionName="1.0" > 


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

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

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <receiver 
      android:name="net.xyz.network.Receiver" 
      android:label="Receiver" > 
      <intent-filter> 
       <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> 
       <action android:name="android.net.wifi.WIFI_STATE_CHANGED" /> 
      </intent-filter> 
     </receiver> 
    </application> 

</manifest> 

これが出力されない原因になっていますか?

+0

を試してみてはトーストについてIDKのが、あなたのショーの活動をし、数秒はそれを終了した後に!それは面白く見えません –

+0

@Flexoありがとうございます。 –

答えて

0

Toast.makeText(context.getApplicationContext(), status, Toast.LENGTH_LONG).show(); 
+0

アクティビティのMainLauncherを割り当てることなく、動作しません... –

関連する問題