简体   繁体   English

如何创建“显示通知”复选框

[英]How can i create a “show notification” checkboxpreferene

How can i create a.simple check box in the preferences screen (a checkboxpreferene) to show a notification?what i mean is that i want enter in my application settings(the preferences screen) and there i'll find the checkbox (show notification).when checked start a notification (for now a text like :my first notification). 我如何在首选项屏幕(复选框首选项)中创建一个简单复选框以显示通知?我的意思是我想在我的应用程序设置(首选项屏幕)中输入,在那里我将找到该复选框(显示通知) )。选中后会启动通知(目前,例如:my第一个通知)。 Obviously when is not checked the notification disappear.i can't find any guide about it. 显然,如果不检查,通知就会消失。我找不到任何有关它的指南。 This is part of my code so far: the preferences screen: 到目前为止,这是我的代码的一部分:首选项屏幕:

public class settings extends PreferenceActivity {

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

        addPreferencesFromResource(R.xml.settings);


    }
}

the settings.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>

and in my MainActivity 在我的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);
            }
    }

But the notification not start when i check the checkbox. 但是当我选中复选框时通知不会开始。

Look at this link : Android SDK: Using Alerts, Toasts and Notifications 查看此链接: Android SDK:使用警报,祝酒和通知

Here is what I have tested and it works (tested on real tablet) : 这是我测试过的并且可以正常工作(在实际平板电脑上测试过):

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

        }
     });
}

} }

I've done it on a button and not a checkbox, but it is the same. 我已经完成了一个按钮而不是一个复选框,但是它是相同的。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM