打开智能手机时保持通知

时间:2016-08-21 23:16:41

标签: android android-studio notifications

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

这是代码:

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 个答案:

答案 0 :(得分:2)

通知不会重新启动。要做出你想要的事情,你需要

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

2)取消通知后将其删除,以免意外重新显示。

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