如何创建"show notification"复选框首选



如何在首选项屏幕中创建一个简单的复选框(一个复选框xpreference)来显示通知?我的意思是,我想在我的应用程序设置(首选项屏幕)中输入,然后我会找到复选框(显示通知)。选中后启动通知(目前是一个文本,如:我的第一个通知)。很明显,如果没有检查,通知就会消失。我找不到任何关于它的指南。到目前为止,这是我代码的一部分:首选项屏幕:

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);
            }
    }

但当我选中复选框时,通知不会启动。

查看此链接:Android SDK:使用警报、Toast和通知

以下是我测试的内容,它有效(在真正的平板电脑上测试):

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" />

最新更新