我正在尝试为我的项目编写代码,该代码用于在WiFi连接断开时播放音乐,并且每当用户单击“测试”按钮时,它就会显示当前连接强度。我尝试了以下代码:
主要活动部分(用于测试按钮,并调用类AlarmManagerBroadcastReceiver:
public class MainActivity extends FragmentActivity {
public static final String TAG = "Final Year Project";
private static boolean wifiConnected = false;
private static boolean mobileConnected = false;
private LogFragment mLogFragment;
private AlarmManagerBroadcastReceiver alarm ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sample_main);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
registerReceiver(alarm, intentFilter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// When the user clicks TEST, display the connection status.
case R.id.test_action:
checkNetworkStrengh();
return true;
// Clear the log view fragment.
case R.id.clear_action:
mLogFragment.getLogView().setText("");
return true;
}
return false;
}
private void checkNetworkStrengh() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo Info = cm.getActiveNetworkInfo();
if (Info == null || !Info.isConnectedOrConnecting()) {
Log.i(TAG, "No connection");
} else {
int netType = Info.getType();
int netSubtype = Info.getSubtype();
if (netType == ConnectivityManager.TYPE_WIFI) {
Log.i(TAG, "Wifi connection");
WifiManager wifiManager = (WifiManager) getApplication().getSystemService(Context.WIFI_SERVICE);
List<ScanResult> scanResult = wifiManager.getScanResults();
for (int i = 0; i < scanResult.size();) {
Log.d("scanResult", "Speed of wifi" + scanResult.get(i).level);//The db level of signal //
// its okay now thanks guys //
}
} else if (netType == ConnectivityManager.TYPE_MOBILE) {
Log.i(TAG, "GPRS/3G connection");
// Need to get differentiate between 3G/GPRS
}
}
}
}
AlarmManagerBroadcastReceiver部分(创建以便始终扫描当前网络连接):
public class AlarmManagerBroadcastReceiver extends BroadcastReceiver {
private static boolean wifiConnected = false;
private static boolean mobileConnected = false;
@Override
public void onReceive(Context context, Intent intent) {
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
NetworkInfo networkInfo =
intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
if (networkInfo.isConnected()) {
// Wifi is connected
Intent in = new Intent(context, RingService.class);
context.stopService(in);
} else {
Intent in = new Intent(context, RingService.class);
context.startService(in);
}
}
}
和MusicService部分(为在类AlarmMAnagerBroadcastReceiver触发特定条件时播放音乐而创建):
public class MusicService extends Service{
private MediaPlayer mp;
@SuppressWarnings("deprecation")
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
mp.start();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
try {
mp.start();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
mp.setOnErrorListener(new MediaPlayer.OnErrorListener() {
public boolean onError(MediaPlayer mp, int what, int extra) {
// TODO Auto-generated method stub
try {
mp.release();
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
});
super.onStart(intent, startId);
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
try {
mp = new MediaPlayer();
mp = MediaPlayer.create(MusicService.this, R.raw.ly);
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
super.onCreate();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
mp.stop();
mp.release();
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
清单文件
<service
android:name="com.example.android.basicnetworking.RingService"
android:exported="true"
android:process=":remote" >
</service>
<receiver android:name=".AlarmManagerBroadcastReceiver" >
<intent-filter android:priority="100" android:enabled="true"
android:label="ConnectivityActionReceiver">
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.STATE_CHANGE" />
</intent-filter>
</receiver>
可以使用Android编程专家来帮助我编写代码,以使我是android和Java编程的新手)可以返回AlarmManagerBroadcastReceiver部分中的checkConnectivity类,并且可以在wifi连接断开时播放音乐。
编辑:感谢您的帮助。我已经知道了。如果有人需要验证码,则可以将其收件箱收录,否则,一旦一切正常,我将上传完整的验证码。
编辑:更新了一些代码
最佳答案
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
int numberOfLevels = 5;
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int level = WifiManager.calculateSignalLevel(wifiInfo.getRssi(), numberOfLevels);
当您尝试再次播放音乐时,则需要再次进行准备,因此每次在开始通话之前也要做好准备:
将此添加到AlarmManagerBroadcastReceiver:
void play(int musicId) {
MediaPlayer mp = MediaPlayer.create(getContext(), musicId);
mp.prepare();
mp.start();
}
并在checkConnectivity中要播放音乐的位置:
play(R.raw.ly); // or use other resource instead of ly