设备重启后服务没有进行午餐......!

时间:2016-11-01 12:28:17

标签: android service broadcastreceiver sharedpreferences

嘿我服务它的壁纸服务(不是动态壁纸)所以当我进入应用程序时,我有5个单选按钮,每个按钮决定壁纸更改的时间,我有一个触发服务的按钮,我得到了我的回复服务器,即使我关闭应用程序,但....当我重新启动设备时,我只得到1个响应,壁纸没有改变让我说我被选中每4秒改变,当我重新启动它停止,就像我说我收到只是来自我的服务器的一个响应,并停止甚至壁纸不会改变这个响应看看我的代码你会理解更多(我使用接收器和启动预测,但...);

MainActivity;

private final static int INTERVAL = 4000; //10 min
private final static int INTERVAL2 = 1000*60*30; // 30 min
private final static int INTERVAL3 = 1000 * 60 * 120; // 2 hours
private final static int INTERVAL4 = 1000 * 60 * 360; // 6 hours min
private final static int INTERVAL5 = 1000 * 60 * 1440; // 1 day

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    rd1 = (RadioButton) findViewById(R.id.radioButton);
    rd2 = (RadioButton) findViewById(R.id.radioButton2);
    rd3 = (RadioButton) findViewById(R.id.radioButton3);
    rd4 = (RadioButton) findViewById(R.id.radioButton4);
    rd5 = (RadioButton) findViewById(R.id.radioButton5);

    radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
    mHandler = new Handler();
    btn = (Button) findViewById(R.id.button);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent(MainActivity.this , WallService.class);
            PendingIntent pintent = PendingIntent.getService(MainActivity.this, 0, intent, 0);
            AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
            alarm.cancel(pintent);
            if (rd1.isChecked()) {

                alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),INTERVAL, pintent);
            } else if (rd2.isChecked()) {
                alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),INTERVAL2, pintent);
            }else if (rd3.isChecked()) {
                alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),INTERVAL3, pintent);
            }else if (rd4.isChecked()) {
                alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),INTERVAL4, pintent);
            }else if (rd5.isChecked()) {
                alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), INTERVAL5, pintent);
            }
        }
    });
}

服务

 String forecastJsonStr;

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

@Override
public void onStart(Intent intent, int startId) {


    final Thread thread1 = new Thread(new Runnable() {
        @Override
        public void run() {
            HttpURLConnection urlConnection = null;
            BufferedReader reader = null;


            try {

                URL url = new URL("yay.php");

                urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setRequestMethod("POST");
                urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                urlConnection.setDoInput(true);
                urlConnection.setDoOutput(true);
                urlConnection.connect();
                DataOutputStream wr = new DataOutputStream(
                        urlConnection.getOutputStream());
                wr.write("method=get_random_wallpaper".getBytes());
                wr.flush();
                wr.close();

                InputStream inputStream = urlConnection.getInputStream();
                StringBuffer buffer = new StringBuffer();
                if (inputStream == null) {
                }
                reader = new BufferedReader(new InputStreamReader(inputStream));

                String line;
                while ((line = reader.readLine()) != null) {

                    buffer.append(line + "\n");
                    Log.d("hey", buffer.toString());

                }

                if (buffer.length() == 0) {
                }
                forecastJsonStr = buffer.toString();

            } catch (IOException e) {
                Log.e("PlaceholderFragment", "Error ", e);

            } finally {
                if (urlConnection != null) {
                    urlConnection.disconnect();
                }
                if (reader != null) {
                    try {
                        reader.close();

                    } catch (final IOException e) {
                        e.printStackTrace();
                    }
                }
            }

        }

    });
    thread1.start();

     Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {

            WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());
            try {

                Bitmap result = Picasso.with(getBaseContext())
                        .load(hostName + hostWallpaperName +   forecastJsonStr)
                        .get();
                wallpaperManager.setBitmap(result);
            } catch (IOException ex) {
                ex.printStackTrace();
            }

        }

    });
    thread.start();
}



@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
}

接收器

 public void onReceive(Context context, Intent intent) {
    if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
        Intent pushIntent = new Intent(context, WallService.class);
        context.startService(pushIntent);

清单

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mike.lol8">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />


<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <receiver android:name=".BootCompletedIntentReceiver"
        android:enabled="true"
        android:exported="true"
        >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
    <service android:name=".WallService"
        android:enabled="true"
        android:exported="true">

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

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

2 个答案:

答案 0 :(得分:0)

在警报管理器中设置的设备重启警报清除后 - docs

您需要在BOOT_COMPLETED接收器上再次初始化Alarm manager。您可以使用sharedPrefference存储设置警报所需的时间 在onClick上添加活动 SharedPreference preference = PreferenceManager.getDefaultSharedPreference(this); preference.edit.putInt("timeInterval",INTERVAL).apply;

然后在接收者的onReceive中:

SharedPreference preference = 

PreferenceManager.getDefaultSharedPreference(this);
int interval = preference.getInt("timeInterval", 4000);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),interval, pintent);

答案 1 :(得分:0)

创建一个接收器并在清单中注册,然后在onReceive()上再次启动服务

TRISA