我有一個BroadcastReceiver
,並在其OnReceive
我應該調用MainActivity
中定義的方法。無論我在哪裏搜索,我發現我將不得不編寫下面的代碼。StartActivity在Broadcastreciever打開應用程序,每次調用
Bundle bundle = intent.Extras;
Intent callingIntent = new Intent(context, typeof(MainActivity));
callingIntent.PutExtra("isSynched", true);
callingIntent.AddFlags(ActivityFlags.NewTask);
callingIntent.AddFlags(ActivityFlags.SingleTop);
context.StartActivity(callingIntent);
現在這調用我的方法,但應用程序不斷打開。我不希望發生這種情況。我想要在應用程序處於後臺並且希望應用程序處於後臺時調用該方法。我該怎麼辦?我正在使用xamarin編寫代碼。 我已經創建了一個獲取數據的服務,但在收到數據後,我必須調用MainActivity中的方法來更新日曆。我目前做的OnRecieve這樣,公衆覆蓋無效
OnReceive(Context context, Android.Content.Intent intent) { if (context.GetType().Equals(typeof(MainActivity))) { ((MainActivity)context).SyncCalendar(); }}
這種情況下就要爲restrictedaccess。所以不能叫SyncCalendar方法
如果您需要在您的應用程序中在後臺執行某些操作,而不顯示活動,請使用服務。 – njzk2