我正在尝试制作一个通知应用程序,当我单击按钮时,它应该在弹出通知时发出哔声。
当我单击按钮时,会出现通知,但运行时出现声音错误。我认为运行时出现“模拟器没有响应”错误。
我收到 android 媒体播放器错误:
07-19 19:32:28.846: I/StagefrightPlayer(110): setDataSource('android.resourse://com.example.status/2130968576') 07-19 19:32:28.846: E/MediaPlayer(198):错误 (1, -2147483648) 07-19 19:32:28.846: W/NotificationService(198): android.reourse://com.example.status/2130968576 的错误加载声音 07-19 19:32:28.846: W /NotificationService(198): java.io.IOException: Prepare failed.: status=0x1 07-19 19:32:28.846: W/NotificationService(198): at android.media.MediaPlayer.prepare(Native Method) 07-19 19:32:28.846: W/NotificationService(198): 在 com.android.server.NotificationPlayer$CreationAndCompletionThread.run(NotificationPlayer.java:90) 07-19 19:32:28.976: D/dalvikvm(273): GC_CONCURRENT 释放701K,55%免费3028K/6599K,外接2434K/2980K,暂停8ms+4ms
这是Java代码以防万一:
package com.example.status;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.support.v4.app.NavUtils;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification notify = new Notification(android.R.drawable.stat_notify_more,
"This is important", System.currentTimeMillis());
Context context = MainActivity.this;
CharSequence title = "You have been notified";
CharSequence details = "Continue with what you are doing";
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);
notify.setLatestEventInfo(context,title,details,pending);
notify.sound = Uri.parse("android.resourse://com.example.status/" + R.raw.beep);
nm.notify(0, notify);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}