2017-04-03 33 views
2

発信コールが行われたときに通知を表示しようとしていますが、動作しません(何も表示されません)。私は別に通知をテストしましたが、うまくいきましたが、私はBroadcastReceiverで何か問題があったと確信しています。 ありがとうございます!Xamarin/Androidアプリ:放送受信機が動作しない

MainActivity:

[Activity(Label = "ExamProject", MainLauncher = true, Icon = "@drawable/icon")] 
public class MainActivity : Activity 
{ 
    protected override void OnCreate(Bundle bundle) 
    { 
     MyBroadcastRecieverClass reciever; 
     base.OnCreate(bundle); 
     // Set our view from the "main" layout resource 
     SetContentView(Resource.Layout.Main); 
     reciever = new MyBroadcastRecieverClass(); 
     RegisterReceiver(reciever, new IntentFilter("android.intent.action.NEW_OUTGOING_CALL")); 

BroadcastRecieverクラス:

namespace App3 
{ 
[BroadcastReceiver(Enabled = true, Exported = true)] 
[IntentFilter(new[] { Android.Content.Intent.ActionNewOutgoingCall })] 
public class MyBroadcastRecieverClass: BroadcastReceiver 
{ 
    public override void OnReceive(Context context, Intent intent) 
    { 
     // Do stuff here. 

     //String value = intent.GetStringExtra("key"); 

     Notification.Builder builder = new Notification.Builder(Application.Context) 
       .SetContentTitle("Test Title") 
       .SetContentText("test content") 
       .SetSmallIcon(Resource.Drawable.Icon) 
       .SetPriority(100) 
       .SetDefaults(NotificationDefaults.Sound) 
       .SetVisibility(NotificationVisibility.Public) 
       .SetCategory(Notification.CategoryAlarm); 


     //build the notification 
     Notification test = builder.Build(); 

     // Get the notification manager: 
     NotificationManager notificationManager = 
      context.GetSystemService(Context.NotificationService) as NotificationManager; 

     // Publish the notification: 
     const int notificationId = 0;//should be changed to generate a uniqe value 
     notificationManager.Notify(notificationId, test); 
    } 
} 

}

+2

あなたは、発信電話を処理する権限を追加しましたか? ( 'PROCESS_OUTGOING_CALLS') – SushiHangover

答えて

0

あなたはlogcatログにすべてのエラーを取得するのですか?

プロジェクトにPROCESS_OUTGOING_CALLS権限を必ず追加してください。

このインテントを受け取るには、PROCESS_OUTGOING_CALLSの許可を保持する必要があります。私は、エラーを発見した

+0

権限が設定されていないことが原因です。どのログを参照しているのかはわかりませんが、Visual Studioのデバッグ出力ログには多くの情報がありますが、ブロードキャストレシーバを参照するものは何も表示されません –

1

、syntaxtの問題がここにあった:

RegisterReceiver(reciever, new IntentFilter("android.intent.action.NEW_OUTGOING_CALL")); 

正しいコード:

public class MainActivity : Activity 
{ 
    protected override void OnCreate(Bundle bundle) 
    { 
     MyBroadcastRecieverClass reciever; 
     base.OnCreate(bundle); 
     // Set our view from the "main" layout resource 
     SetContentView(Resource.Layout.Main); 
     reciever = new MyBroadcastRecieverClass(); 

     IntentFilter myIntentFilter = new IntentFilter("android.intent.action.NEW_OUTGOING_CALL"); 
     RegisterReceiver(reciever, myIntentFilter); 

     Button settingsButton = FindViewById<Button>(Resource.Id.settingsButton); 
関連する問題