Android 重新启动时检测HTC通电
所以我遇到了一个问题,我需要能够确定在HTC设备上重启。我可以确定关机,但检测完全重启似乎更具挑战性。在包括了所有可能的安卓权限和意图过滤器后,我认为我已经得出结论,我肯定错过了HTC的一些东西。我是否需要HTC的许可或其他东西来直接决定手机重启Android 重新启动时检测HTC通电,android,permissions,broadcastreceiver,restart,Android,Permissions,Broadcastreceiver,Restart,所以我遇到了一个问题,我需要能够确定在HTC设备上重启。我可以确定关机,但检测完全重启似乎更具挑战性。在包括了所有可能的安卓权限和意图过滤器后,我认为我已经得出结论,我肯定错过了HTC的一些东西。我是否需要HTC的许可或其他东西来直接决定手机重启 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 同样,无论是重启还是完全硬关机/加电,ShutdownReceiver都做
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
同样,无论是重启还是完全硬关机/加电,ShutdownReceiver都做得很好。但是BootReceiver只检测到硬通电,而没有检测到HTC重启。所以当我重新启动时,我可以告诉你我关机了,但它从未检测到启动完成。。。有什么想法吗 如果您可以判断关机事件是否发生,并且既然您可以订阅该事件,为什么不将两者结合起来? 在应用程序SharedPreferences上保存上次发生“关机”事件的日期-在“启动完成”事件之后,将日期与保存的日期进行比较-如果时差小于(比如)5分钟,请执行“重新启动”操作 我添加了一个适用于我的代码示例(运行android 4.3的galaxy S4)。我没有主activitis,但这些不是你的示例所必需的(只需抛出一个空白示例或将服务和引导加载程序复制到你的…)
舱单-
<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 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-如果您愿意,请给我发送一个电子邮件地址,我会将整个项目发送给您。问题是,我从未在重启时为我的接收器点击BOOT_COMPLETED。它可以判断它关闭了,但没有检测到它正在重新启动,因此用户必须手动启动应用程序,这与我想做的相反:)你确定吗?你可能做错了什么-你可以使用下面的教程作为指导-我之前测试过,你可以在重启事件后继续调试-上面引导完成的线程对我有效..我100%确定。如果我使用“关机”关闭手机,然后按住电源按钮(就像大多数普通Android设备一样)重新启动手机,那么我会检测到开机完成。我唯一没有检测到BOOT_COMPLETED的时间是当它从重启完成引导时。因此,一般的引导功能已经完成,这是一个特定的问题。我添加了一些代码exmaple-它可以正常工作,再加上手动重启(关闭设备而不是打开设备)在res>raw文件中抛出一个虚拟声音,您应该会听到一个声音。如果那不起作用,我很抱歉,但我没有主意=\谢谢你的代码,但我仍然没有检测到“定期重启”,他们只是检测到“手动重启”。它只是在重新启动时无法识别通电。它必须是清单中的某种东西,因为它控制着我的接收者是如何实际实现的。许可或贴错标签的包裹或其他东西。
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();
}
}