我想创建一个没有Activity 的Android应用程序,并且需要通过 intent 来启动该应用程序。
用例1:从另一个Android应用程序调用该应用程序时,我遇到了问题,因为该应用程序没有被调用,并且控制台中也没有错误日志。
用例2:我正在尝试在RECEIVE_BOOT_COMPLETED上进行操作。这样,它也无法正常工作
要作为服务清单文件运行的应用程序:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="something.boo.com.diagnosticservicepai" android:sharedUserId="android.uid.system">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name">
<receiver
android:name=".BootDeviceReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<service
android:name=".SomeService"
android:exported="true"></service>
</application>
</manifest>
BootDeviceReceiver.java :
public class BootDeviceReceiver extends BroadcastReceiver {
private static final String TAG_BOOT_BROADCAST_RECEIVER = "BOOT_SomeService";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
String message = "BootDeviceReceiver onReceive, action is " + action;
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
Log.d(TAG_BOOT_BROADCAST_RECEIVER, action);
if(Intent.ACTION_BOOT_COMPLETED.equals(action))
{
startServiceDirectly(context);
//startServiceByAlarm(context);
}
}
/* Start RunAfterBootService service directly and invoke the service every 10 seconds. */
private void startServiceDirectly(Context context)
{
try {
//while (true) {
String message = "BootDeviceReceiver onReceive start service directly.";
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
Log.d(TAG_BOOT_BROADCAST_RECEIVER, message);
// This intent is used to start background service. The same service will be invoked for each invoke in the loop.
Intent startServiceIntent = new Intent(context, SomeService.class);
context.startService(startServiceIntent);
// Current thread will sleep one second.
Thread.sleep(10000);
//}
}catch(InterruptedException ex)
{
Log.e(TAG_BOOT_BROADCAST_RECEIVER, ex.getMessage(), ex);
}
}
/* Create an repeat Alarm that will invoke the background service for each execution time.
* The interval time can be specified by your self. */
private void startServiceByAlarm(Context context)
{
// Get alarm manager.
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
// Create intent to invoke the background service.
Intent intent = new Intent(context, SomeService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
long startTime = System.currentTimeMillis();
long intervalTime = 60*1000;
String message = "Start service use repeat alarm. ";
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
Log.d(TAG_BOOT_BROADCAST_RECEIVER, message);
// Create repeat alarm.
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, startTime, intervalTime, pendingIntent);
}
}
SomeService.java:
public class SomeService extends Service {
private static final String TAG_BOOT_EXECUTE_SERVICE = "BOOT_SomeService";
private someAIDLImpl impl = new someAIDLImpl();
MediaPlayer player;
private class SomeAIDLImpl extends ISomeBaseService.Stub{
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG_BOOT_EXECUTE_SERVICE, "SomeService onCreate() method.");
}
@Override
public IBinder onBind(Intent intent) {
Log.v(this.getClass().getName(), "onBind(..)");
//return null;
return impl;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("MESS", "onStartCommand call");
String message = "SomeService onStartCommand() method.";
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
Log.d(TAG_BOOT_EXECUTE_SERVICE, "SomeService onStartCommand() method.");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
}
/*private void startService(Context context) {
if(rc < 0){
Log.v(this.getClass().getName(), "failed");
return;
}else {
Log.v(this.getClass().getName(), "success ");
}
Log.v(this.getClass().getName(), "Service started at "+ new java.sql.Timestamp(System.currentTimeMillis()).toString());*//*
player = MediaPlayer.create(this, Settings.System.DEFAULT_RINGTONE_URI);
player.setLooping(true);
player.start();
}*/
}
答案 0 :(得分:0)
如果您想接收BOOT_COMPLETED
,则必须进行一项活动。最初安装时,在强制关闭后,一个应用程序位于"stopped" state中,直到用户启动其活动之一。当应用处于停止状态时,将不会调用任何广播接收器。