Android 如何在接收时使用广播接收机
我有一个项目,当我运行应用程序时,一个服务应该是活动的,当设备打开时,我的android服务应该是活动的。 该项目在emulator中成功运行,但在我的手机中,当我打开设备时,它不工作 我的广播接收器:Android 如何在接收时使用广播接收机,android,Android,我有一个项目,当我运行应用程序时,一个服务应该是活动的,当设备打开时,我的android服务应该是活动的。 该项目在emulator中成功运行,但在我的手机中,当我打开设备时,它不工作 我的广播接收器: public class BroadcastReceiverOnTurnedOn extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Int
public class BroadcastReceiverOnTurnedOn extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, MyService.class);
context.startService(startServiceIntent);
}
}
public class BroadcastReceiverOnTurnedOn extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, MyService.class);
context.startService(startServiceIntent);
}
}
我补充说:
<receiver android:name="com.dariran.BroadcastReceiverOnTurnedOn">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name="com.dariran.MyService"
android:enabled="true"
android:exported="true" >
</service>
<receiver android:name="com.dariran.BroadcastReceiverOnTurnedOn"
android:enabled="true">
<intent-filter android:priority="1">
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" />
</intent-filter>
</receiver>to appliation tag on Manifest.xml and
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
在Manifest.xml和
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
您正在注册广播接收器,而不是服务。如果内存和资源允许,服务通常会一直在后台运行
您的广播接收器仅在设备重新启动时激活,并且模拟器在每次启动时启动。安装应用程序时,手机不会启动
您应该注册AppInstalled之类的事件,或者实际实施一项服务,如下所示:
您需要确保,一旦在设备上安装应用程序,它应该通过单击应用程序图标启动一次,然后只有启动接收器会在随后的重新启动呼叫中接收启动事件。我取消重新启动,如果我在内部存储中安装应用程序成功,即使设备重新启动,但当我在外部存储上安装应用程序时,然后重新启动设备它不工作 我有一个项目,当运行应用程序时,一个服务是活动的,当设备打开时,我的android服务是活动的。该项目在设备的内部内存中成功运行,但当我在外部内存中安装并重新启动设备时,该项目无法运行 首先,我在first Activity中调用我的服务,它可以工作,但当我重新启动设备时,它就不能工作了 我的活动:
public class FirstClass extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
final Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
public void run()
{
startService(new Intent(getApplicationContext(), MyService.class));
startActivity(new Intent(FirstClass.this, MainActivity.class));
finish();
}
},5000);
}
/////////////////////////////////////////////////
}
我的广播接收器:
public class BroadcastReceiverOnTurnedOn extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, MyService.class);
context.startService(startServiceIntent);
}
}
public class BroadcastReceiverOnTurnedOn extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, MyService.class);
context.startService(startServiceIntent);
}
}
我补充说:
<receiver android:name="com.dariran.BroadcastReceiverOnTurnedOn">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name="com.dariran.MyService"
android:enabled="true"
android:exported="true" >
</service>
<receiver android:name="com.dariran.BroadcastReceiverOnTurnedOn"
android:enabled="true">
<intent-filter android:priority="1">
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" />
</intent-filter>
</receiver>to appliation tag on Manifest.xml and
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
它在第一个活动的onCreate上启动,但当我运行应用程序时,它工作了,当我重新启动设备时,它不工作了。您能检查一下您是否在清单文件中注册了服务吗?或者只需将日志放在引导接收器中,引导接收器中至少执行代码。您的意思是我应该首先在活动上定义并实现服务??是-用户离开活动后,您正在运行进程。。这意味着您需要一个服务来处理结果,然后,如果用户仍在您的应用程序中,请让该服务通知活动i undrestand,我在手机内存上安装了应用程序并重新启动了设备服务,现在当应用程序在外接内存上时,我如何运行它?