问题描述
我该如何使用广播接收机和服务两者,前提是应用程序安装在外接存储。我编程,但只是在内部存储设备上运行,因为我希望在设备重新启动服务运行,而不是启动活动
我的活动:
公共类的Firstclass扩展活动
{
公共无效的onCreate(包savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.first);
最终的处理程序处理程序=新的处理程序();
handler.postDelayed(新的Runnable()
{
公共无效的run()
{
startService(新意图(getApplicationContext(),MyService.class));
startActivity(新意图(FirstClass.this,MainActivity.class));
完();
}
},5000);
}
/////////////////////////////////////////////////
}
我的广播接收器:
公共类BroadcastReceiverOnTurnedOn扩展的BroadcastReceiver {
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
意图startServiceIntent =新的意图(背景下,MyService.class);
context.startService(startServiceIntent);
}
}
我说:
<服务
机器人:名称=com.dariran.MyService
机器人:启用=真
机器人:出口=真正的>
< /服务>
<接收器的Android版本:NAME =com.dariran.BroadcastReceiverOnTurnedOn
机器人:启用=真正的>
<意图过滤器的Android版本:优先级=1>
<作用机器人:名称=android.intent.action.BOOT_COMPLETED/>
&所述; /意图滤光器>
<意向滤光器>
<作用机器人:名称=android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE/>
&所述; /意图滤光器>
< /接收器>
上的Manifest.xml蒋云良标签和我说这code我的服务类把一个过滤器来识别外部存储但不工作了:(
@覆盖
公共无效ONSTART(意向意图,诠释startId){
尝试 {
IntentFilter的过滤器=新的IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
BroadcastReceiver的mReceiver =新BroadcastReceiverOnTurnedOn();
registerReceiver(mReceiver,过滤器);
}赶上(例外五){
}
}
引用的:
在的文件,这个时候 ACTION_EXTERNAL_APPLICATIONS_AVAILABLE
:
因此,你需要设置你的应用程序无法在外部存储安装。
how can i use broadcast receiver and service both of them, provided that app install on external storage. i programmed it but just run on internal storage device because i want when device rebooted service run instead start activity
my 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);
}
/////////////////////////////////////////////////
}
my broadcast receiver :
public class BroadcastReceiverOnTurnedOn extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, MyService.class);
context.startService(startServiceIntent);
}
}
i added :
<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 andi added this code to my service class to put a filter to recognize the external storage but don't work again :(
@Override
public void onStart(Intent intent, int startId) {
try {
IntentFilter filter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
BroadcastReceiver mReceiver = new BroadcastReceiverOnTurnedOn();
registerReceiver(mReceiver, filter);
} catch (Exception e) {
}
}
Quoting the documentation:
Quoting elsewhere in the documentation, this time for ACTION_EXTERNAL_APPLICATIONS_AVAILABLE
:
Hence, you will need to set up your app to not be installed on external storage.
这篇关于如何使用广播接收器与服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!