我有一个项目,当运行应用程序时,一个服务处于活动状态,当设备启动时,我的android服务处于活动状态。 项目在设备的内部存储器中成功运行但是当我将其安装在外部存储器上并重新启动我的设备时,不能正常工作!
首先我在第一个活动中呼叫我的服务并且它可以工作,但是当我重新启动我的设备时它不起作用!
我的活动:
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);
}
}
我补充说:
<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>
到Manifest.xml上的appliation标签和
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
答案 0 :(得分:0)
我将此代码添加到我的服务类中,以设置过滤器来识别外部存储但不再工作:(
@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) {
}
}