【问题标题】:How can i create a "show notification" checkboxpreferene我怎样才能创建一个“显示通知”checkboxpreferene
【发布时间】:2013-07-15 07:14:38
【问题描述】:

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

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 checkbox notifications android-preferences


    【解决方案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" />
    

    【讨论】:

    • 我的问题是从首选项中的复选框创建通知。在教程中解释了如何创建通知而不是通过首选项。
    • 所以您的意思是,使用我的代码,您可以通过首选项中的复选框启用/禁用通知?编辑:哦,好吧,那我用按钮试试你的代码..
    • 但是你的代码都在MainActivity里面。我的有点不同,我使用偏好类。
    • 好的,谢谢。你认为我可以试试我的 settings.xml 吗?因为 settimgs.xml 是一个首选项布局,并且按钮/复选框等的状态已经“保存”,而不使用 java 中的 sharedpreferences。
    猜你喜欢
    • 2017-08-11
    • 2020-01-17
    • 2011-04-27
    • 2017-07-06
    • 2022-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-01
    相关资源
    最近更新 更多