我正试图构建一个运行在后台并对来电进行激活的应用程序,在一些研究之后,我发现我必须自己动手,但我的代码根本不做任何事情。
如果有办法在PCL项目上这样做,请告诉我。我正在使用一个服务和一个广播接收器。以下是我的实际代码:
[Activity(Label = "Teste2", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
public static Context AppContext;
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
AppContext = this.ApplicationContext;
StartPushService();
}
public static void StartPushService()
{
AppContext.StartService(new Intent(AppContext, typeof(Services.BackgroundService)));
if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Kitkat)
{
PendingIntent pintent = PendingIntent.GetService(AppContext, 0, new Intent(AppContext, typeof(Services.BackgroundService)), 0);
AlarmManager alarm = (AlarmManager)AppContext.GetSystemService(Context.AlarmService);
alarm.Cancel(pintent);
}
}
public static void StopPushService()
{
AppContext.StopService(new Intent(AppContext, typeof(Services.BackgroundService)));
PendingIntent pintent = PendingIntent.GetService(AppContext, 0, new Intent(AppContext, typeof(Services.BackgroundService)), 0);
AlarmManager alarm = (AlarmManager)AppContext.GetSystemService(Context.AlarmService);
alarm.Cancel(pintent);
}
}
服务:
[Service(Name = "com.xamarin.Teste2.BackgroundService")]
public class BackgroundService : Service
{
// Magical code that makes the service do wonderful things.
public override void OnCreate()
{
base.OnCreate();
}
public override StartCommandResult OnStartCommand(Android.Content.Intent intent, StartCommandFlags flags, int startId)
{
return StartCommandResult.Sticky;
}
public override Android.OS.IBinder OnBind(Android.Content.Intent intent)
{
return null;
}
public override void OnDestroy()
{
base.OnDestroy();
}
}
和BroadcastReceiver:
[BroadcastReceiver]
[IntentFilter(new[] { Android.Content.Intent.ActionAnswer })]
public class CallReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
Toast.MakeText(context, "Incoming call from someone", ToastLength.Short).Show();
System.Console.WriteLine("Incoming call from someone");
}
}
发布于 2017-11-23 14:48:15
这是不可能的,因为IOS不允许
https://stackoverflow.com/questions/46794858
复制相似问题