我很抱歉,我不是一位经验丰富的Android开发人员,我正在使用C#在Xamarin中开发一个Android项目。我希望这个问题不是重复的,因为我似乎找不到一个,但如果是的话,请标记并我会愉快地删除这个问题。
当我的应用程序启动并运行时,我希望它的droid应用程序图标出现在通知栏中。当应用程序进入销毁事件时,我希望该图标和消息被删除。到目前为止,我好像已经实现了这一点,但是我似乎找不到或无法弄清楚如何通过点击通知消息将我的运行中的应用程序带到前景(仅在尚未运行时)。我认为我对此的代码方向不正确,可能涉及到待定意图之类的东西?以下是我的代码。也许我接近了,也许我走错了方向,但是任何人可以给予的任何帮助或指针都将不胜感激。
[Activity(Label = "MyActivity", MainLauncher = true, Theme = "@android:style/Theme.NoTitleBar")]
public class MainActivity : Activity
{
Notification notification = null;
NotificationManager notificationManager = null;
const int notificationId = 0;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
Notification.Builder builder = new Notification.Builder(this)
.SetContentTitle("My App is Running")
.SetContentText("Show in forground")
.SetSmallIcon(Resource.Drawable.Icon);
// Build the notification:
notification = builder.Build();
// Get the notification manager:
notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;
// Publish the notification:
notificationManager.Notify(notificationId, notification);
}
protected override void OnDestroy()
{
Log.Debug(logTag, "Location app is becoming inactive");
notificationManager.Cancel(notificationId);
base.OnDestroy();
}
}