gpt4 book ai didi

android - 我如何创建一个 "show notification"checkboxpreferene

转载 作者:行者123 更新时间:2023-11-30 03:29:47 24 4
gpt4 key购买 nike

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

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

关于android - 我如何创建一个 "show notification"checkboxpreferene,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17648630/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com