gpt4 book ai didi

android - 打开智能手机时保留通知

转载 作者:行者123 更新时间:2023-11-29 19:37:04 25 4
gpt4 key购买 nike

我有这个应用程序来创建通知,但我需要在智能手机打开/重启时保留它们。我试图创建一个服务并在智能手机打开时自动启动应用程序,但没有用。我能做什么?

这是代码:

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;


public class MainActivity extends Activity {

Button guardar, cancelar;
EditText nota;
ListView lista;
ArrayList<String> notas;
ArrayAdapter<String> adaptador;
NotificationManager notificationManager;
SharedPreferences preferencias;
SharedPreferences.Editor editor;
int cpos; //cantidad de posiciones ya guardadas
Intent intent;
PendingIntent pIntent;
Notification n;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

lista = (ListView) findViewById(R.id.lista);
notas = new ArrayList<String>();
adaptador = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, notas);
lista.setAdapter(adaptador);

preferencias = getSharedPreferences("notas", 0);
editor = getSharedPreferences("notas", 0).edit();

cpos = preferencias.getInt("posicionesT", -1);

for(int i = 0; i <= cpos; i++)
{
notas.add(preferencias.getString(Integer.toString(i), ""));
adaptador.notifyDataSetChanged();
}

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.

if(item.getItemId() == R.id.add)
{
LayoutInflater inflater = getLayoutInflater();
View dialog = inflater.inflate(R.layout.nota, null);

guardar = (Button) dialog.findViewById(R.id.guardar);
cancelar = (Button) dialog.findViewById(R.id.cancelar);
nota = (EditText) dialog.findViewById(R.id.nota);

cancelar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
}
});

guardar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
notas.add(nota.getText().toString());
adaptador.notifyDataSetChanged();

cpos++;
editor.putInt("posicionesT", cpos);
editor.putString(Integer.toString(cpos), nota.getText().toString());
editor.commit();

intent = new Intent(getApplicationContext(), MainActivity.class);
pIntent = PendingIntent.getActivity(getApplicationContext(), (int) System.currentTimeMillis(), intent, 0);

n = new Notification.Builder(getApplicationContext())
.setContentTitle("Recordar")
.setStyle(new Notification.BigTextStyle().bigText(nota.getText()))
.setSmallIcon(R.drawable.ic_icono)
.setContentIntent(pIntent)
.setColor(Color.parseColor("#4CAF50"))
.setOngoing(true) //notificacion persistente
.addAction(R.drawable.ic_borrar, "Quitar nota", pIntent).build();


notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

notificationManager.notify(cpos, n);
}
});

AlertDialog.Builder alerta = new AlertDialog.Builder(MainActivity.this);
alerta.setView(dialog);
alerta.show();
}

return super.onOptionsItemSelected(item);
}

}

最佳答案

通知不会持续重启。为了让你想要的发生,你需要

1)当你得到一个通知时,将通知写入磁盘(共享首选项、数据库、文件等)

2)取消通知时将它们移除,以免意外重新显示。

3) 编写一个 BOOT_COMPLETE 广播接收器。让它读取存储的通知并创建具有相同信息的新通知

关于android - 打开智能手机时保留通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39069652/

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