1

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

public class settings extends PreferenceActivity {

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

        addPreferencesFromResource(R.xml.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);
            }
    }

但是当我选中复选框时通知不会开始。

4

1 回答 1

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

于 2013-07-15T07:18:00.073 回答