我正在尝试构建一个提醒应用程序。 在此应用程序中,用户将选择保存在手机上的WiFi名称。当用户连接到该WiFi时,手机将振动。电话振动后,服务就会消失。
我尝试编写服务以每1分钟检查一次WiFi名称。当我关闭应用程序时,它会再次运行,但不会像我预期的那样。
服务实施:
public class AlarmService extends Service {
private static final String TAG = "com.wificheckalways";
public static String wifiToCheck = MainActivity.wifiName;
public AlarmService(){
}
public int onStartCommand(Intent intent, int flags, int startId){
Log.i(TAG, "onStartCommand called");
Runnable runnable = new Runnable() {
@Override
public void run() {
String ssid = "";
ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (networkInfo.isConnected()) {
final WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
if (connectionInfo != null && !TextUtils.isEmpty(connectionInfo.getSSID())) {
ssid = connectionInfo.getSSID();
}
}
while(!wifiToCheck.equals(ssid)){
Log.i(TAG, "Goes to SLEEP");
synchronized (this){
try{
Thread.sleep(60000);
Log.i(TAG, "WAKE UP");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (networkInfo.isConnected()) {
final WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
if (connectionInfo != null && !TextUtils.isEmpty(connectionInfo.getSSID())) {
ssid = connectionInfo.getSSID();
Log.i(TAG, "Compare " + wifiToCheck + " with " + ssid);
}
}
}
if(wifiToCheck.equals(ssid)) {
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(2000); // 2000 miliseconds = 2 seconds
}
}
};
Thread alarmThread = new Thread(runnable);
alarmThread.start();
return Service.START_STICKY; //Restart service if it got destroyed
}
public void onDestroy(){
Log.i(TAG, "onDestroyCommand called");
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
我在MainActivity.java中启动服务的地方:
public void SetAlarm(View view) {
if (!wifiName.equals("")) {
Toast.makeText(getApplicationContext(), "Alarm set for: " + wifiName, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this, AlarmService.class);
startService(intent);
}
else{
Toast.makeText(getApplicationContext(), "Please choose a WiFi name!!!", Toast.LENGTH_SHORT).show();
}
}
wifiName是选择的Wifi。
此代码有什么问题?
答案 0 :(得分:1)
正如我在评论中所建议的,您的服务可能如下所示:
public class AlarmService extends Service {
public AlarmService() {}
@Override
public void onCreate() {
super.onCreate();
IntentFilter intentFilter = new IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION);
registerReceiver(receiver, intentFilter);
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
if (networkInfo.isConnected()) {
if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
String ssid = wifiManager.getConnectionInfo().getSSID();
if (ssid.equals(<<ENTERED_SSID>>)) {
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(2000);
stopSelf();
}
}
}
}
}
};
}
唯一缺少&#34;&#34;#34;输入SSID。
答案 1 :(得分:0)
我猜您必须在onPause()
onDestroy()
或stopService(new Intent(MainActivity.this,AlarmService.class));
方法中停止服务