我有完全相同的problem。挂起意图触发的Android通知在设备附带调试器时完美无效,但在删除后无法正常工作。该问题中给出的解决方案对我不起作用。
代码
public class MedicineReceiver extends BroadcastReceiver {
private static int notificationId=0;
private static int id=0;
DataBase dataBase;
public MedicineReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
//throw new UnsupportedOperationException("Not yet implemented");
if(id!=intent.getIntExtra("ID",-1))
{
notificationId++;
id=intent.getIntExtra("ID",-1);
}
dataBase=new DataBase(context);
if(dataBase.isOkay(id+""))
{
Intent myIntent=new Intent(context,Medicine_ItemActivity.class);
myIntent.putExtra("ID",intent.getIntExtra("ID",-1));
PendingIntent pendingIntent=PendingIntent.getActivity(context,id,myIntent,PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.medicine)
.setContentTitle("ঔষধ ")
.setContentText(createText(intent))
.setAutoCancel(true)
.setLights(Color.RED, 3000, 3000)
.setVibrate(new long[]{1000,1000,1000})
.setContentIntent(pendingIntent);
if (getBool(dataBase.getSound(id + ""))) {
String source = dataBase.getSourcse(id + "");
if(source.compareTo("DEFAULT")==0)
{
Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mBuilder.setSound(uri);
}
else
{
mBuilder.setSound(getURI(source, context));
}
}
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(notificationId, mBuilder.build());
}
}
Uri getURI(String st,Context context)
{
File ringtoneFile = new File(st); //sd-card path/songname.mp3
ContentValues content = new ContentValues();
content.put(MediaStore.MediaColumns.DATA,ringtoneFile.getAbsolutePath());
content.put(MediaStore.MediaColumns.TITLE, getFileName(st));
content.put(MediaStore.MediaColumns.SIZE, ringtoneFile.length());
content.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
// content.put(MediaStore.Audio.Media.ARTIST, "Madonna");
content.put(MediaStore.Audio.Media.IS_RINGTONE, false);
content.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
content.put(MediaStore.Audio.Media.IS_ALARM, false);
content.put(MediaStore.Audio.Media.IS_MUSIC, false);
Uri Ringtone1 = Uri.parse(st);
//Insert it into the database
Log.i("TAG", "the absolute path of the file is :"+
ringtoneFile.getAbsolutePath());
Uri uri = MediaStore.Audio.Media.getContentUriForPath(
ringtoneFile.getAbsolutePath());
context.getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + ringtoneFile.getAbsolutePath() + "\"",
null);
Uri newUri = context.getContentResolver().insert(uri, content);
System.out.println("uri=="+uri);
Log.i("TAG","the ringtone uri is :"+newUri);
return newUri;
}
String getFileName(String st)
{
String []pathFile=st.split("/");
return pathFile[pathFile.length-1];
}
boolean getBool(String st)
{
if(st.compareTo("true")==0)
{
return true;
}
else
{
return false;
}
}
String createText(Intent intent)
{
String text="আপনার "+dataBase.getName(id+"")+" ঔষধটি ";
if(intent.getStringExtra("MORNING")!=null)
{
text+="(সকালে";
}
if(intent.getStringExtra("NOON")!=null)
{
text+=",দুপুরে";
}
if(intent.getStringExtra("NIGHT")!=null)
{
text+=",রাতে";
}
text+=")খাওয়ার সময় হয়েছে।";
return text;
}
}
清单声明
<receiver
android:name=".Starting.Medicine.MedicineReceiver"
android:enabled="true"
android:exported="true" />