2017-03-04 14 views
1

私は自分自身のサービス(RadarService)をアクティビティで見つけたいと思っています - サービスが動作しているかどうかを確認してください。私自身のサービスを見つけるには?

しかし、名前は、errm、ちょっと混乱していて、私は失われています。サービスを開始するために、私は意図を作成します。

this.radarIntent = new Intent(this, typeof(RadarService)); 

だから私はこの意図からサービスの名前を抽出し、比較のためにそれを使用しようとした - しかし、Classプロパティは意図自体のクラスの名前を返します。 、Typeプロパティは空です。

typeof(RadarService).ToString() - これは私に文字列MyNamespace.RadarService、niceを使用しようとしました。しかし、私が失敗した実行中のサービスの一覧と一致させようとしたとき、私のサービスはmd5--here-comes-md5-hash--.RadarService(にClassNameShortClassNameと設定されています)と記載されています。

私自身のサービスを見つけるにはどうすればいいですか?

答えて

3

typeofC#タイプ/名前を与えるために起こっているあなたはそのCanonicalNameを得ることができるので、あなたはそのC#型の自動生成JavaAndroidの呼び出し可能ラッパークラスをしたいです。

Java.Lang.Class.FromType(typeof(StackOverFlowService)).CanonicalName) 

例:

[Service(Label = "StackOverFlowService", Name="com.sushihangover.WickedApp.StackOverFlowService")] 
[IntentFilter(new String[] { "com.sushihangover.StackOverFlowService" })] 
public class StackOverFlowService : Service 
{ 
~~~ 
} 

var intent = new Intent(this, typeof(StackOverFlowService)); 
StartService(intent); 

var serviceName = Java.Lang.Class.FromType(typeof(StackOverFlowService)).CanonicalName; 
var manager = (ActivityManager)GetSystemService(ActivityService); 
foreach (var item in manager.GetRunningServices(int.MaxValue)) 
{ 
    if (item.Service.ClassName == serviceName) 
     Log.Debug("SO", "Service is running!!!"); 
} 

あなたはXamarin.AndroidがACWベースのクラス属性にNameパラメータで名前をハードコーディングでないこと自動MD5ベースのJavaクラスの命名を回避することができます

サービスJavaクラス名は、md58b0fd40f68fa0d8c16b76771789ed62a.StackOverFlowService

の代わりに com.sushihangover.WickedApp.StackOverFlowServiceになります。
+0

ありがとうございました!また、サービスの属性に関する情報。 – greenoldman

関連する問題