我想讓我的第一個Android地理位置和本地通知系統應用程序。 我想像這樣...有基本的活動MainActivity
。啓動後,它啓動服務TestService
,如果座標發生變化,則將其發送到服務器上,並在回覆中收到一些將顯示爲本地通知的消息。 我有一些問題。地理位置,服務和本地通知
如果我關閉應用程序(使用任務管理器),那麼服務將停止,所以座標改變後沒有任何反應。 我需要做什麼服務一直工作?或者這是不可能的?
激活本地通知後,它啓動
NotifyActivity
顯示詳細信息。在那裏我點擊buttonDelete
- 它將關閉NotifyActivity
並開始MainActivity
。但如果在此之後我切換到操作系統屏幕(使用Back
按鈕)並返回(使用任務管理器),則將再次顯示'MainActivity'而不顯示'NotifyActivity'。 爲什麼會出現以及如何避免它?
MainActivity
[Activity(Label = "LocationTest", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
var button = FindViewById<Button>(Resource.Id.myButton);
button.Click += delegate {
StartService(new Intent(this, typeof(TestService)));
button.Text = "Started";
};
}
}
地理定位服務
[Service]
public class TestService : Service, ILocationListener
{
// Start service
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
locManager = (LocationManager)GetSystemService(LocationService);
locationCriteria = new Criteria();
locationCriteria.Accuracy = Accuracy.Coarse;
locationCriteria.PowerRequirement = Power.Low;
string locationProvider = locManager.GetBestProvider(locationCriteria, true);
// Preferences.MinTime, for example, 60 (seconds)
// Preferences.MinDist, for example, 100 (meters)
locManager.RequestLocationUpdates(locationProvider, Preferences.MinTime * 1000, Preferences.MinDist, this);
return StartCommandResult.Sticky;
}
public void OnLocationChanged(Location loc)
{
// Send coordinates to the server, receive a response, and show local notification
var msg = new ReceivedMessage(counter++, "Test Title", loc.ToString());
ShowNotification(msg);
}
// show local notification
void ShowNotification(ReceivedMessage msg)
{
var myContainer = new Bundle();
myContainer.PutLong("msg_id", Convert.ToInt64(msg.Id));
myContainer.PutStringArray("msg_data", new [] { msg.Title, msg.Text });
var resultIntent = new Intent(this, typeof(NotifyActivity));
resultIntent.PutExtras(myContainer);
TaskStackBuilder stackBuilder = TaskStackBuilder.Create(this);
stackBuilder.AddParentStack(Java.Lang.Class.FromType(typeof(NotifyActivity)));
stackBuilder.AddNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.GetPendingIntent(Convert.ToInt32(msg.Id), PendingIntentFlags.UpdateCurrent);
Notification.Builder builder = new Notification.Builder(this)
.SetDefaults(NotificationDefaults.Sound | NotificationDefaults.Vibrate)
.SetAutoCancel(true)
.SetContentIntent(resultPendingIntent)
.SetContentTitle(msg.Title)
.SetContentText(msg.Text)
.SetSmallIcon(Resource.Drawable.Icon);
var nm = (NotificationManager)GetSystemService(NotificationService);
nm.Notify(Convert.ToInt32(msg.Id), builder.Build());
}
}
本地通知
[Activity(Label = "NotifyActivity")]
public class NotifyActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.NotifyActivity);
var msg_id = Intent.Extras.GetLong("msg_id");
var msg_data = Intent.Extras.GetStringArray("msg_data");
FindViewById<TextView>(Resource.Id.textTitle).Text = msg_data[0];
FindViewById<TextView>(Resource.Id.textDescription).Text = msg_data[1];
FindViewById<Button>(Resource.Id.buttonDelete).Click += delegate {
StartActivity(typeof(MainActivity));
Finish();
};
}
}
示例項目here
我認爲你在這兩個賬戶上都有些不對。首先,我不認爲OP正在討論強制關閉流程,我認爲他們指的是從最近的應用程序列表中輕掃應用程序,應該只關閉並刪除該任務堆棧,而不是停止整個過程。其次,在Xamarin中,活動是使用活動類本身的屬性聲明的。然後它們會在編譯時自動放入清單中。 – irreal