Implémentation de weakfulIntentService dans xamarin.android. Problème lors de la lecture du fichier de métadonnées. eturn null lors de la lecture du fichier xml metadatafile dans BroadCastReceiver.Erreur lors de la lecture du fichier metadata à l'aide de activityInfo.LoadXmlMetaData()
** lecteur XmlReader = activityInfo.LoadXmlMetaData (PackageManager, WAKEFUL_META_DATA) **;
BroadcastReceiver
namespace Squarelabs.Droid
{
[BroadcastReceiver]
public class AlarmReceiver : BroadcastReceiver
{
private static string WAKEFUL_META_DATA = "squrelabs.inspection";
public override void OnReceive(Context context, Intent intent)
{
WakefulIntentService.IAlarmListener alarmListener =
GetListener(context);
if (alarmListener != null)
{
if (intent.Action == null)
{
alarmListener.SendWakefulWork(context);
}
else
{
WakefulIntentService.ScheduleAlarms(alarmListener, context, true);
}
}
}
private WakefulIntentService.IAlarmListener GetListener(Context context)
{
PackageManager packageManager = context.PackageManager;
ComponentName componentName = new ComponentName(context, Class);
try
{
ActivityInfo activityInfo = packageManager.GetReceiverInfo(componentName, PackageInfoFlags.MetaData);
XmlReader reader = activityInfo.LoadXmlMetaData(packageManager, WAKEFUL_META_DATA);
while (reader!=null)
{
if (reader.IsStartElement())
{
if (reader.Name == "WakefulIntentService")
{
string className = reader.Value;
Class cls = Java.Lang.Class.ForName(className);
return ((WakefulIntentService.IAlarmListener)cls.NewInstance());
}
}
reader.MoveToNextAttribute();
}
} catch (NameNotFoundException e) {
throw new RuntimeException("Cannot find own info???", e);
} catch (XmlPullParserException e) {
throw new RuntimeException("Malformed metadata resource XML", e);
} catch (IOException e) {
//throw new RuntimeException("Could not read resource XML", e);
} catch (ClassNotFoundException e) {
throw new RuntimeException("Listener class not found", e);
} catch (IllegalAccessException e) {
throw new RuntimeException("Listener is not public or lacks public constructor", e);
} catch (InstantiationException e) {
throw new RuntimeException("Could not create instance of listener", e);
}
return (null);
}
}
manifeste
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto" android:versionCode="2" android:versionName="1.9.5" package="squrelabs.inspection">
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:label="Squarelabs" android:icon="@drawable/icon">
<receiver android:name="AlarmReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<meta-data android:name="squrelabs.inspection" android:resource="@xml/wakeful" />
</receiver>
</application>
xml en ressources
<WakefulIntentService
listener="squrelabs.inspection.Droid.AppListener"
/>
bien vouloir me aider à résoudre ce problème.
Merci. Essayons. –
@SwapnilWakchaure, et essayez de supprimer le récepteur de votre manifeste et en attendant ajouter l'attribut 'IntentFilter' sur votre' AlarmReceiver', par exemple: '[BroadcastReceiver (Enabled = true)] [IntentFilter (new [] {" com .xamarin.example.TEST "})] [MetaData (" mTag ", Value =" @ chaîne/WakefulIntentService ")]' –
Obtention ClassNotFoundException. En ligne Classe cls = Java.Lang.Class.ForName (mTag); –