Asynctask,BroadcastReceiver和Service

时间:2017-11-12 10:40:04

标签: java android android-asynctask alarmmanager android-things

使用闹钟管理器我试图调用asynctask。首先,我执行警报的所有配置,当警报响起时,它调用广播接收器,然后是服务,最后是AsyncTask。但是我有一个错误,说无法创建服务:零没有参数。我相信问题出现在清单上,也许我正在创建错误的服务......

主要活动:

public class HomeActivity extends Activity {


@Override

protected void onCreate(Bundle savedInstanceState) {

    WifiManager wifiManager;

    super.onCreate(savedInstanceState);

    wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);

    wifiManager.setWifiEnabled(true); //Initialize wifi

    AlarmManager alarmMgr = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); //Create alarm
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(this, YourAlarmReceiver.class),PendingIntent.FLAG_CANCEL_CURRENT);



    if (wifiManager.isWifiEnabled()==false)  //Check if wifi is on
    {
        Log.d("ERROR", "WIFI OFF");
    }
    else
    {
        Log.e("WiFI","Wifi ON");

        alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(),90000, pendingIntent);

        Log.e("ALRM","ALARM CREATED");
    }

    //new SendRequest().execute();
}

服务,BroadcastReceiver和Asynctask

public class ServiceAux extends Service{
    public ServiceAux(){
        super();
    }
    public void onCreate(){
        new SendRequest().execute();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
public static class YourAlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e("SERVICE","SERVICE CREATED");
        context.startService(new Intent(context, ServiceAux.class));
    }
}

public class SendRequest extends AsyncTask<String, Void, String> { ... } //Send information to a server

我的清单

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application>
    <uses-library android:name="com.google.android.things" />


    <activity android:name=".HomeActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

            <category android:name="android.intent.category.IOT_LAUNCHER" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <receiver android:name=".HomeActivity$YourAlarmReceiver" android:enabled="true"></receiver>
    <service android:name=".HomeActivity$ServiceAux"></service>
</application>

0 个答案:

没有答案