0

この質問は、私の前の質問に基づいています:Android-通知が消えないように設定します

通知をトリガーするには、起動時に何かをトリガーする必要があることに気付きました。

前の質問から、2つのルートを取ることができるようです。サービスを開始するか、BroadcastReceiverを使用してください。サービスを開始するのは少しやり過ぎだと思います(しかし、私は何を知っていますか)。BroadcastReceiverルートを使用しているときにスタックします。それは一般的には機能しますが、通知のためにコードを取得すると、たくさんのエラーが発生します。

これは私のコードです:

NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle("My notification")
                    .setContentText("Hello World!");
            // Creates an explicit intent for an Activity in your app
            Intent resultIntent = new Intent(this, ResultActivity.class);
            //Intent.putExtra("My Notification");
            // The stack builder object will contain an artificial back stack for the
            // started Activity.
            // This ensures that navigating backward from the Activity leads out of
            // your application to the Home screen.
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
            // Adds the back stack for the Intent (but not the Intent itself)
            stackBuilder.addParentStack(ResultActivity.class);
            // Adds the Intent that starts the Activity to the top of the stack
            stackBuilder.addNextIntent(resultIntent);
            PendingIntent resultPendingIntent =
                    stackBuilder.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT
                    );
            mBuilder.setContentIntent(resultPendingIntent);
            NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            // mId allows you to update the notification later on.
            mNotificationManager.notify(i, mBuilder.build());

私のエラー: ここに画像の説明を入力してください

これについて何かアイデアはありますか?

概要:サービスまたは放送受信機の方が優れていますか(この状況では)?コード内のこれらのエラーを解決するにはどうすればよいですか(ホバーすると、未定義と表示されます)?

4

1 に答える 1

4

thisエラー行のすべてのインスタンスを。に置き換えますcontext

BroadcastRecieverは、ActivityandServiceとは異なりコンテキストを実装しません。エラーが発生したすべてのメソッドには、コンテキストのインスタンスが必要です

于 2012-10-16T21:02:49.380 に答える