所以我遇到了一个问题,我需要能够确定HTC设备上的重启。我可以确定ShutDown,但检测完全重启似乎更具挑战性。在包含所有可能的Android权限和意图过滤器之后,我可以想到我已经得出结论,我必须遗漏HTC中的某些内容。我是否需要从HTC直接确定手机重启的权限?
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
...
<!-- RECEIVERS -->
<receiver android:name="com.smashingboxes.speed.BootReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON" />
<!-- Just adding this to show that I have tried everything :P -->
<action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver android:name="com.smashingboxes.speed.ShutdownReceiver" >
<intent-filter>
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
<action android:name="com.htc.intent.action.QUICKBOOT_POWEROFF" />
</intent-filter>
</receiver>
同样,ShutdownReceiver无论是重启还是完全硬停电/上电都可以做到这一点。但BootReceiver只检测硬启动而不是HTC的重启。因此,当我重新启动时,我可以告诉我关闭,但它从未检测到BOOT_COMPLETED ...任何想法?
答案 0 :(得分:0)
如果您可以告诉关闭事件发生,并且因为您可以订阅"ON-BOOT-COMPLETED"事件 - 为什么不将它们结合起来呢? 最后一次“关闭”事件发生时,在您的应用程序SharedPrefernces上保存日期 - 在“启动完成”事件之后将日期与保存的日期进行比较 - 如果时差低于(每个说)5分钟就可以了“重启“行动。
我添加了一个适用于我的代码示例(运行android 4.3的galaxy S4)我没有主要的活动,但这些对于你的例子来说并不是必需的(只需抛出空白或将服务和引导程序复制到你的... ) -
清单 -
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.servicelifecycle"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".StartedServiceActivity"
android:label="@string/title_activity_main" >
</activity>
<service android:name=".StartedService" >
</service>
<service android:name="BoundService" >
</service>
<activity android:name="BoundServiceActivity" >
</activity>
<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="BoundedServiceUsingMessangerInterface" >
</service>
<activity android:name="MessangerServiceActivity" >
</activity>
<activity android:name="IntentServiceActivity" >
</activity>
<service android:name="IntentServiceDemo" >
</service>
<service android:name="ForeGroundServiceDemo" >
</service>
<activity android:name="ForeGroundServiceActivity" >
</activity>
<activity android:name=".AIDLActivity" >
</activity>
<service
android:name="BoundedServiceUsingAIDL"
android:exported="false" >
<intent-filter>
<action android:name="com.example.bindservice.AIDL" />
</intent-filter>
</service>
<receiver android:name=".BootCompletedReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
服务 -
package com.example.servicelifecycle;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class StartedService extends Service {
private static final String tag = StartedService.class.getSimpleName();
Timer timer;
ToastHandler t;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
t = new ToastHandler(getApplicationContext());
t.showToast(tag + " onCreate", 3000);
Log.d(tag, "onCreate");
timer = new Timer();
timer.scheduleAtFixedRate(new RemindTask(), 10000, 6000);
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
t.showToast(tag + " onCreate", 3000);
Log.d(tag, "onStartCommand startId=" + startId);
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
timer.cancel();
Log.d(tag, "onDestroy");
super.onDestroy();
}
@Override
public boolean onUnbind(Intent intent) {
t.showToast(tag + " onCreate", 3000);
Log.d(tag, "onUnbind");
return super.onUnbind(intent);
}
class RemindTask extends TimerTask {
public void run() {
t.play();
}
}
}
BootReciever -
package com.example.servicelifecycle;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class BootCompletedReceiver extends BroadcastReceiver {
final static String TAG = BootCompletedReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
Log.w(TAG, "starting service...");
context.startService(new Intent(context, StartedService.class));
}
}
要运行的一些示例任务
package com.example.servicelifecycle;
import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Handler;
import android.widget.Toast;
/**
* A class for showing a <code>Toast</code> from background processes using a
* <code>Handler</code>.
*
* @author kaolick
*/
public class ToastHandler {
// General attributes
private Context mContext;
private Handler mHandler;
/**
* Class constructor.
*
* @param _context
* The <code>Context</code> for showing the <code>Toast</code>
*/
public ToastHandler(Context _context) {
this.mContext = _context;
this.mHandler = new Handler();
}
/**
* Runs the <code>Runnable</code> in a separate <code>Thread</code>.
*
* @param _runnable
* The <code>Runnable</code> containing the <code>Toast</code>
*/
private void runRunnable(final Runnable _runnable) {
Thread thread = new Thread() {
public void run() {
mHandler.post(_runnable);
}
};
thread.start();
thread.interrupt();
thread = null;
}
/**
* Shows a <code>Toast</code> using a <code>Handler</code>. Can be used in
* background processes.
*
* @param _resID
* The resource id of the string resource to use. Can be
* formatted text.
* @param _duration
* How long to display the message. Only use LENGTH_LONG or
* LENGTH_SHORT from <code>Toast</code>.
*/
public void showToast(final int _resID, final int _duration) {
final Runnable runnable = new Runnable() {
@Override
public void run() {
// Get the text for the given resource ID
String text = mContext.getResources().getString(_resID);
Toast.makeText(mContext, text, _duration).show();
}
};
runRunnable(runnable);
}
/**
* Shows a <code>Toast</code> using a <code>Handler</code>. Can be used in
* background processes.
*
* @param _text
* The text to show. Can be formatted text.
* @param _duration
* How long to display the message. Only use LENGTH_LONG or
* LENGTH_SHORT from <code>Toast</code>.
*/
public void showToast(final CharSequence _text, final int _duration) {
final Runnable runnable = new Runnable() {
@Override
public void run() {
Toast.makeText(mContext, _text, _duration).show();
}
};
runRunnable(runnable);
}
/**
* Shows a <code>Toast</code> using a <code>Handler</code>. Can be used in
* background processes.
*
* @param _text
* The text to show. Can be formatted text.
* @param _duration
* How long to display the message. Only use LENGTH_LONG or
* LENGTH_SHORT from <code>Toast</code>.
*/
public void play() {
final Runnable runnable = new Runnable() {
@Override
public void run() {
playSound();
}
};
runRunnable(runnable);
}
private void playSound() {
MediaPlayer mp;
// mp = MediaPlayer.create(mContext,);
mp = MediaPlayer.create(mContext, R.raw.click);
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.release();
}
});
mp.setVolume(100, 100);
mp.start();
}
}
PS - 如果你想 - 给我发一个电子邮件地址,我会发给你整个项目。