我的应用程序与广播接收器和WiFi

时间:2014-04-07 11:21:22

标签: android

我,这是我的Android应用程序,我试图与硬件通信,因为对我来说这很令人兴奋!

当我禁用wifi或我改变网络....我需要做吐司和振动。它仅在avd 2.3.3上运行。为什么?我已经尝试过avd 4.1.2,关于三星Galaxy s2 4.1.2,关于avd 4.4.2,关于nexus 7 4.4.2,但没什么......

的AndroidManifest.xml

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

<uses-sdk 
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.VIBRATE"/>

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

<receiver 
android:name="dado.wiibc.Receiver">
<intent-filter>
    <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
</application>

</manifest>

receiver.java

package dado.wifibc;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Vibrator;
import android.widget.Toast;

public class Receiver extends BroadcastReceiver { 

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

Toast.makeText(context, "blablabla!!!.",Toast.LENGTH_LONG).show();

    // Vibrate the mobile phone
Vibrator vibrator = (Vibrator)     context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(2000);
}
}

1 个答案:

答案 0 :(得分:0)

在清单文件中使用所有这些权限。

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