2013-07-15 2 views
1

환경 설정 화면 (checkboxpreferene)에 간단한 알림 상자를 만들어 알림을 표시하려면 어떻게해야합니까? 내 응용 프로그램 설정 (환경 설정 화면) 그리고 거기에 체크 표시 (표시 알림)를 찾습니다. 확인을 클릭하면 알림을 시작합니다 (지금은 내 첫 번째 알림과 같은 텍스트). 분명히 알림이 사라질 때 알림이 사라집니다 .i 그것에 대한 안내서를 찾을 수 없습니다. 이 지금까지 내 코드의 일부입니다 환경 설정 화면 :어떻게 "show notification"체크 박스를 만들 수 있습니까?

public class settings extends PreferenceActivity { 

    @SuppressWarnings("deprecation") 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     addPreferencesFromResource(R.xml.settings); 


    } 
} 

Settings.XML의이

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > 
    <PreferenceCategory 
      android:summary="@string/menu_settings" 
      android:title="@string/Settings"> 
      <CheckBoxPreference 
       android:key="firstDependent" 
       android:summary="@string/clicca_il_primo_check" 
       android:title="@string/Primocheck" 
       android:defaultValue="false" 

      /> 

    </PreferenceCategory> 
<!--Any other categories include here--> 
</PreferenceScreen> 

내 MainActivity에

@SuppressLint("NewApi") 
    private void checkPref(Intent intent){ 
     SharedPreferences myPref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this); 
     boolean pref_opt1 = myPref.getBoolean("firstDependent", false); 
     if (pref_opt1){ 
      NotificationManager notifi = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
      Notification notification = new Notification.Builder(getApplicationContext()) 
      .setContentTitle("Hello Informations") 
      .setContentText("Hellow world") 
      .setSmallIcon(R.drawable.icon_small_not) 
      .setTicker("Helloooo") 
      .build(); 

      notification.flags = Notification.FLAG_ONGOING_EVENT; 
      Intent i = new Intent(MainActivity.this, MainActivity.class); 
      PendingIntent penInt = PendingIntent.getActivity(getApplicationContext(), 0 , i , 0); 
      notifi.notify(215,notification); 
      } else { 
      NotificationManager notifi = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
      notifi.cancel(215); 
      } 
    } 

그러나 알림이 시작되지 나는 체크 박스를 체크한다. 이 링크에서

답변

1

봐 : 여기 Android SDK: Using Alerts, Toasts and Notifications

내가 테스트 한 것이며 (실제 태블릿에서 테스트) 작품 :

public class MainActivity extends PreferenceActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Button button = (Button) findViewById(R.id.button); 
    button.setOnClickListener(new Button.OnClickListener() { 
    public void onClick(View v){ 
     final int NOTIF_ID = 1234; 
     NotificationManager notifManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
     Notification note = new Notification(R.drawable.ic_launcher, "New E-mail", System.currentTimeMillis()); 
     PendingIntent intent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(getApplicationContext(), MainActivity.class), 0); 
     note.setLatestEventInfo(getApplicationContext(), "New E-mail", "You have one unread message.", intent); 
     notifManager.notify(NOTIF_ID, note); 
     // notifManager.cancel(NOTIF_ID); 

     } 
    }); 
} 

가}

나는 버튼을 한 적이 체크 박스가 아니라 동일한 것입니다.

<?xml version="1.0" encoding="utf-8"?> 

<uses-sdk 
    android:minSdkVersion="11" 
    android:targetSdkVersion="17" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="com.example.za.MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/hello_world" /> 

<ListView 
    android:id="@android:id/list" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    ></ListView> 

<Button 
    android:id="@+id/button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/hello_world" /> 

+0

내 문제는 자습서를 preferences.in 것은 아니지만 통해 알림을 만드는 방법 설명에 체크 박스에서 알림을 만드는 것입니다 환경 설정. –

+0

내 첫 번째 게시물을 보시려면 –

+0

어쩌면 알림 코드가 정확하지 않을 수 있습니다. –

관련 문제