2016-06-02 8 views
-4
public class Settings extends AppCompatActivity { 

CheckBox cb1; 

NotificationManager manager; 
Notification myNotication; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_settings); 
    cb1=(CheckBox) findViewById(R.id.notificationchk); 


    manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);// anything here and above seems ok(no error) 

    cb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton toggleButton, boolean isChecked) { 
      NotificationCompat.Builder builder = new NotificationCompat.Builder(this)//"this" taskstackbuilder cannot be applied to oncheckedlistener 
        .Builder(mContext)//the mcontext is not working 
        .setContentTitle("New mail from ") 
        .setContentText("i") 
        .setSmallIcon(R.drawable.ic_launcher) 
        .build();Intent resultIntent = new Intent(this, firstpage.class);//everything inside bracket underlined red 
     TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);//"this" taskstackbuilder cannot be applied to oncheckedlistener 

     stackBuilder.addParentStack(Settings.class); 
     stackBuilder.addNextIntent(resultIntent); 
     PendingIntent resultPendingIntent = 
       stackBuilder.getPendingIntent(
         0, 
         PendingIntent.FLAG_UPDATE_CURRENT 
       ); 
     mBuilder.setContentIntent(resultPendingIntent);//cannot resolve symbol mBuilder 
     NotificationManager mNotificationManager = 
       (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

     mNotificationManager.notify(mId, mBuilder.build());//cannot resolve symbol mID,mBuilder 

    }//all errors marked 


});//basically this,mbuilder,mID,mContext problem 

私はJavaとAndroidアプリケーションの開発に慣れていません。私は何時間もインターネットを検索して通知とチェックボックスを一緒に働かせ、最終的には失敗しました。 あなたのすばらしい答えをありがとう:Dチェックボックスをオンにしたときに通知を送信する方法は?

答えて

1

Settings.this

に変更し、これをmcontextを有する第二のビルダーを削除します。

この内側の意図と同じです。 TaskStackBuilder.create(Settings.this)

変更あなたがちょうどあなたのポスト内のコードスニペットに含める逃した場合を除きmNotificationManager.notify(1, builder.build())

+0

Sir、(1).setSmallIconを変更すると、必要なandroid.support.v7.app.NotificationCompat.Builderがandroid.app.notificationを見つけました。 (3)そして、私が最初のページであることを意図していればどうでしょうか?設定ではなく、ジャバ? - Ze Qian Mok 1時間前 –

+0

インテントはすでにfirstpage.javaです。インテントでは、2つのパラメータを指定します。最初は呼び出しアクティビティ、その後は着陸アクティビティです。 "Intent resultIntent =新しいインテント(Settings.this、firstpage.class); 'その意図が設定から最初のページにあることを意味します。 setsmalliconについては、すでに設定しています。エラーが表示されている場合は、上から通知のインポートをクリアし、正しいパッケージを使用して再度インポートしてください –

1

通知を作成しています。しかし、あなたはまだNotifcationManagerに通知していません。例のためにofficial docsをチェックしてください。おそらくこのようなものがありませんか?

NotificationManager mNotificationManager = 
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
mNotificationManager.notify(mId, mBuilder.build()); 
+0

にこのmBuilder.setContentIntent(resultPendingIntent)

builder.setContentIntent(resultPendingIntent)に変更mNotificationManager.notify(mId, mBuilder.build())からTaskStackBuilder.create(this)変更それと同じSettings.this

にそれを変更? @ZeQianMokを投稿してください。 –

関連する問題