電話の電源を入れたときに自動的に実行されるプログラムを作成したいと思います。私はこのプログラムからの通知を、特定の時期に毎日表示したいと思っています。ユーザーはこれらの通知のタイミングを設定します。私はこれをデータベースにsqliteで登録しています。そして私はそれについて書きましたプログラムで放送受信機とサービスを定義しました。保留中のインテントを使用して通知を作成しています。電話がオフになると、ユーザーが生成している通知が消えています。私のクラスは、このようなものです:スタートアッププログラムのアクセス許可をxamarinに書き込む方法
クラスBootCompleted:BroadcastReceiver
クラスAlarmReceiver:BroadcastReceiver
クラス自動起動:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:label="myprogram">
<receiver
android:name=".ReminderService.BootCompleted"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<service android:name=".ReminderService.AutoStartUp">
</service>
</application>
:私のAndroidManifestでサービス
、私は次の定義を作りました
その結果、このプログラムを電話にインストールして試したところ、問題が発生しました。 したがって、ユーザーとして通知を作成して電話をオン/オフすると、「プログラムが機能しなくなりました」というエラーが表示されます。 なぜこれが起こりますか?
マイBootCompletedクラスは
[BroadcastReceiver]
[IntentFilter(new[] { Android.Content.Intent.ActionBootCompleted })]
public class AlarmReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
var message = intent.GetStringExtra("message");
var title = intent.GetStringExtra("title");
var notIntent = new Intent(context, typeof(MainActivity));
notIntent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask);
var contentIntent = PendingIntent.GetActivity(context, 0, notIntent,
PendingIntentFlags.CancelCurrent);
var manager = NotificationManagerCompat.From(context);
var style = new NotificationCompat.BigTextStyle();
style.BigText(message);
var builder = new NotificationCompat.Builder(context)
.SetContentIntent(contentIntent)
.SetSmallIcon(Resource.Drawable.icon)
.SetContentTitle(title)
.SetContentText(message)
.SetStyle(style)
.SetAutoCancel(true);
var notification = builder.Build();
manager.Notify(DateTime.Now.Millisecond, notification);
}
}
マイ自動起動クラスは、拡張サービス
ある[BroadcastReceiver]
[IntentFilter(new[] { Android.Content.Intent.ActionBootCompleted })]
public class BootCompleted : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
Log.Debug("BroadcastReceiverBoot", "BootCompleted");
if (intent.Action.Equals("android.intent.action.BOOT_COMPLETED"))
{
Intent serviceIntent = new Intent(context, typeof(AutoStartUp));
context.StartService(serviceIntent);
}
}
}
マイAlarmReceiverクラスが拡張BroadcastReceiverでBroadcastReceiverを拡張である
class AutoStartUp : Service
{
public override IBinder OnBind(Intent intent)
{
return null;
}
public override void OnCreate()
{
try
{
Toast.MakeText(this, "boot1", ToastLength.Long).Show();
AndroidReminderService.BootRemind(DateTime.Now, "Boot Control", "Phone Opened!", DateTime.Now.Hour, DateTime.Now.Minute + 2, DateTime.Now.Second);
Toast.MakeText(this, "boot2", ToastLength.Long).Show();
}
catch (Exception e)
{
Toast.MakeText(this, e.Message, ToastLength.Long).Show();
throw;
}
}
}
完全なコードなしで伝えるのは難しいです。どこでもクラッシュする可能性があります... –
私はクラスの内容をコメントとして共有します – oflu1071
それはすべて私のコードです。私はこのプログラムをクロスプラットフォームでも書いています。これは複雑なアンドロイド部分に書いたコードです。 – oflu1071