gpt4 book ai didi

java - Android - 广播接收器不记录消息

转载 作者:太空宇宙 更新时间:2023-11-04 12:30:48 25 4
gpt4 key购买 nike

我有一个服务,其中包含一个广播接收器,它会查找 Wi-Fi 状态更改并在启用或禁用时进行提示。但是,我的代码不会烘烤 wi-fi 状态更改消息,尽管它会烘烤与服务创建、启动和销毁相关的消息。

WLANService.java

    public class WLANService extends Service {
public WLANService() {
}

@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "Service Created!", Toast.LENGTH_LONG).show();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

int state = 0;
String message = intent.getStringExtra("message");
Toast.makeText(this, "Service Started & " + message, Toast.LENGTH_LONG).show();

BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if(wifiManager.isWifiEnabled()) {
Log.i("Wi-Fi-State", "Wi-Fi is On!");
}else {
Log.i("Wi-Fi-State", "Wi-Fi is Off!");
}
}
};
return START_STICKY;
}

@Override
public void onDestroy() {
Toast.makeText(this, "Service Destroyed!", Toast.LENGTH_LONG).show();
}
}

MainActivity.java

    public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void startTheService(View v) {
Intent intent = new Intent(this, WLANService.class);
intent.putExtra("message", "I got your message!");
startService(intent);
}

public void stopTheService(View v) {
stopService(new Intent(this, WLANService.class));
}
}

AndroidManifest.xml

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

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

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

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

<service
android:name=".WLANService"
android:enabled="true"
android:exported="false">
</service>
</application>

</manifest>

最佳答案

你可以这样做:

public class WLANService extends Service {
private final BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
boolean connected = info.isConnected();
if(connected) {
Toast.makeText(context, "WIFI CONNECTED!", Toast.LENGTH_LONG).show();
Log.i("Wi-Fi-State", "Wi-Fi is On!");
} else {
Toast.makeText(context, "WIFI DISCONNECTED!", Toast.LENGTH_LONG).show();
Log.i("Wi-Fi-State", "Wi-Fi is Off!");
}
}
}

public WLANService() {
}

@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "Service Created!", Toast.LENGTH_LONG).show();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// registering your receiver
registerReceiver(receiver, new IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION));
return START_STICKY;
}
}

主要 Activity :

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// starting your Service
startTheService();
}

public void startTheService() {
Intent intent = new Intent(this, WLANService.class);
intent.putExtra("message", "I got your message!");
startService(intent);
}
}

关于java - Android - 广播接收器不记录消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37858348/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com