2016-06-02 4 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 app deveopement를 처음 사용합니다. 나는 여러 시간 동안 인터넷을 검색하여 알림과 체크 박스를 함께 사용하여 궁극적으로 실패했습니다. 멋진 답변 주셔서 감사합니다. D확인란을 선택하면 알림을 보내는 방법은 무엇입니까?

답변

1

변경이 mcontext와 두 번째 빌더를 제거합니다.

내부 의도와 동일합니다. TaskStackBuilder.create(Settings.this)

변화 방금 게시물의 코드에 포함하는 놓친하지 않는 한 mNotificationManager.notify(1, builder.build())

+0

선생님, (1) .setSmallIcon을 변경할 때 필요한 android.support.v7.app.NotificationCompat.Builder가 android.app.notification을 찾았습니다. (3) 그리고 만약 내가 그 의도를 첫 페이지로하고 싶다면. - Ze Qian Mok 1 시간 전 –

+0

의도는 이미 firstpage.java입니다. 의도에서 우리는 두 가지 매개 변수를 지정합니다. 먼저 호출 활동 다음에 착수 활동을 지정합니다. 그래서 'Intent resultIntent = 새로운 인 텐트 (Settings.this, firstpage.class);' 인 텐트가 설정에서 첫 페이지에 있음을 의미합니다. setsmallicon에 관해서는 이미 설정했습니다. 오류가 표시되면 알림 가져 오기를 지우고 올바른 패키지를 사용하여 다시 가져옵니다. –

1

알림을 작성 중입니다. 그러나 아직 NotifcationManager님께 알리지 않았습니다. 예를 들어 official docs을 확인하십시오. 다음과 같은 내용이 누락되었을 수 있습니다. Settings.this

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 –

관련 문제