Я запускаю Apache на Ubuntu, и моя проблема в том, что файл /etc/apache2/mods-available/php5.conf
отсутствовал:
<FilesMatch ".+\.ph(p[345]?|t|tml)$">
SetHandler application/x-httpd-php
</FilesMatch>
Я добавил его обратно, а php правильно обрабатывал файлы php.
любое намерение, которое вы делаете для службы, активности и т. д. в вашем приложении, должно всегда следовать этому формату
Intent serviceIntent = new Intent(context,MyService.class);
context.startService(serviceIntent);
или
Intent bi = new Intent("com.android.vending.billing.InAppBillingService.BIND");
bi.setPackage("com.android.vending");
неявным намерениям (что у вас есть ваш код в настоящее время) считаются угрозой безопасности
Для ленивых людей, подобных мне, просто используйте
public static Intent createExplicitFromImplicitIntent(Context context, Intent implicitIntent) {
// Retrieve all services that can match the given intent
PackageManager pm = context.getPackageManager();
List<ResolveInfo> resolveInfo = pm.queryIntentServices(implicitIntent, 0);
// Make sure only one match was found
if (resolveInfo == null || resolveInfo.size() != 1) {
return null;
}
// Get component info and create ComponentName
ResolveInfo serviceInfo = resolveInfo.get(0);
String packageName = serviceInfo.serviceInfo.packageName;
String className = serviceInfo.serviceInfo.name;
ComponentName component = new ComponentName(packageName, className);
// Create a new intent. Use the old one for extras and such reuse
Intent explicitIntent = new Intent(implicitIntent);
// Set the component to be explicit
explicitIntent.setComponent(component);
return explicitIntent;
}
. Отправьте контекст и ваше намерение в этом методе и получите намерение использовать результат.
Я улучшил ответ Шахидула, чтобы удалить зависимость от контекста:
public class ServiceUtils {
public static void startService(String intentUri) {
Intent implicitIntent = new Intent();
implicitIntent.setAction(intentUri);
Context context = SuperApplication.getContext();
Intent explicitIntent = convertImplicitIntentToExplicitIntent(implicitIntent, context);
if(explicitIntent != null){
context.startService(explicitIntent);
}
}
private static Intent convertImplicitIntentToExplicitIntent(Intent implicitIntent, Context context) {
PackageManager pm = context.getPackageManager();
List<ResolveInfo> resolveInfoList = pm.queryIntentServices(implicitIntent, 0);
if (resolveInfoList == null || resolveInfoList.size() != 1) {
return null;
}
ResolveInfo serviceInfo = resolveInfoList.get(0);
ComponentName component = new ComponentName(serviceInfo.serviceInfo.packageName, serviceInfo.serviceInfo.name);
Intent explicitIntent = new Intent(implicitIntent);
explicitIntent.setComponent(component);
return explicitIntent;
}
}
Внутри класса SuperApplication:
public class SuperApplication extends Application {
private static MyApp instance;
public static SuperApplication getInstance() {
return instance;
}
public static Context getContext(){
return instance;
// or return instance.getApplicationContext();
}
@Override
public void onCreate() {
instance = this;
super.onCreate();
}
}
В вашем манифесте:
<application
android:name="com.example.app.SuperApplication "
android:icon="@drawable/icon"
android:label="@string/app_name"
.......
<activity
......
И тогда просто вызовите:
ServiceUtils.startService("com.myservice");
Установите packageName
.
intent.setPackage(this.getPackageName());
Попробуйте это. меня устраивает. Здесь MonitoringService - мой класс обслуживания. У меня есть два действия, которые указывают на остановку или запуск службы. Я посылаю это значение из моего широковещательного приемника в зависимости от AIRPLANE_MODE_CHANGED.
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(Intent.ACTION_AIRPLANE_MODE_CHANGED.equalsIgnoreCase(action)){
boolean isOn = intent.getBooleanExtra("state", false);
String serviceAction = isOn? MonitoringService.StopAction : MonitoringService.StartAction;
Intent serviceIntent = new Intent(context, MonitoringService.class);
serviceIntent.setAction(serviceAction);
context.startService(serviceIntent);
}
}
ПРИМЕЧАНИЕ. Я добавляю следующий код для запуска моего широковещательного приемника с именем: ManageLocationListenerReceiver.
<receiver
android:name=".ManageLocationListenerReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.AIRPLANE_MODE" />
</intent-filter>
</receiver>
bindService
? – Igor Ganapolsky 14 July 2016 в 18:06