2017-10-19 76 views
0

我想用PCL AddData();中的方法帮助执行操作,但是我正在寻找一种方法将它附加到下面的通知AddAction的属性上:在Xamarin(PCL)执行方法的通知上添加操作

.AddAction(Resource.Drawable.tick_notify, "Take", pIntent) 

,但我不知道如何,因为参数需要一个PendingIntent连线出来,是否有从PCL加入AddData方法的一种方式? 执行某个操作?

EDITED

Service.cs

using System; 
using Android.App; 
using Android.Content; 
using Android.OS; 

namespace Diabetes.Droid 
{ 
    [Service] 
    public class Mservice : Service 
    { 
     //Receiver class 
     private AlarmReceiver mReceiver = new AlarmReceiver(); 
     /// <summary> 
     /// Raises the bind event. 
     /// </summary> 
     /// <param name="intent">Intent.</param> 
     public override IBinder OnBind(Intent intent) 
     { 
      return null; 
     } 



     /// <summary> 
     /// Called by the system when the service is first created. 
     /// </summary> 
     public override void OnCreate() 
     { 
      base.OnCreate(); 
      //IntentnFilter all actities to receive information 
      IntentFilter iF = new IntentFilter(); 
      iF.AddAction("com.android.music.metachanged"); 
      iF.AddAction("com.android.music.playstatechanged"); 
      iF.AddAction("com.android.music.playbackcomplete"); 
      iF.AddAction("com.android.music.queuechanged"); 
      iF.AddAction("com.htc.music.metachanged"); 
      iF.AddAction("fm.last.android.metachanged"); 
      iF.AddAction("com.sec.android.app.music.metachanged"); 
      iF.AddAction("com.nullsoft.winamp.metachanged"); 
      iF.AddAction("com.amazon.mp3.metachanged"); 
      iF.AddAction("com.miui.player.metachanged"); 
      iF.AddAction("com.real.IMP.metachanged"); 
      iF.AddAction("com.sonyericsson.music.metachanged"); 
      iF.AddAction("com.rdio.android.metachanged"); 
      iF.AddAction("com.samsung.sec.android.MusicPlayer.metachanged"); 
      iF.AddAction("com.andrew.apollo.metachanged"); 
      //register receiver with intentnfilter 
      RegisterReceiver(mReceiver, iF); 
     } 
     public override void OnDestroy() 
     { 
      base.OnDestroy(); 
     } 
    } 
} 

回答

1

没有没有办法来直接方法链接到通知操作。

当然是因为通知是在系统手机界面上运行的,而不是你的应用程序代码。

你应该建立一个挂起的意图,并使用接收器来接收回调。

这个限制在Xamarin中以相同的方式存在。

你可以找到谷歌样本中为例(在标准Android的Java代码)UniversalMusicPlayer#MediaNotificationManager.java

创建你的意图:建立你的通知时

mAddDataPendingIntent = PendingIntent.getBroadcast(mService, REQUEST_CODE, 
       new Intent(ACTION_ADD_DATA).setPackage(pkg), PendingIntent.FLAG_CANCEL_CURRENT); 

然后:

notificationBuilder.addAction(new NotificationCompat.Action(icon, label, mAddDataPendingIntent)); 

注册您的播放:

IntentFilter filter = new IntentFilter(); 
       filter.addAction(ACTION_ADD_DATA); 
       mService.registerReceiver(this, filter); 

和接收操作:

@Override 
public void onReceive(Context context, Intent intent) { 
    final String action = intent.getAction(); 
    LogHelper.d(TAG, "Received intent with action " + action); 
    switch (action) { 
     case ACTION_ADD_DATA: 
      addData(); 
      break; 
    } 
} 
+0

根据你的代码,我可以代替'mTransportControls.play();'和'AddData();'方法。 –

+0

我面对的另一个问题是Java,如果您提供了C#代码 –

+0

是的,这是在哪里调用您的方法。我已经更新了我的答案,以便与您的背景更相关。关于C#版本,抱歉,但我对xamarin的知识太弱,无法为您提供完整的示例。 – smora

相关问题