我有一個應用程序,其中一個玩家活動是通過通知發起的。在玩家活動中有兩個按鈕。我已經實現的onclick監聽器上的每個按鈕,但它不是在API 8onclick在api中工作正常18但不在api上8
工作Player.java
public class Player extends BaseActivity implements OnPreparedListener, MediaPlayerControl {
private static final String TAG = "AudioPlayer";
Button done , snz;
PendingIntent pendingIntent;
String name, alrname;
WebView wv;
RelativeLayout r1;
public static final String AUDIO_FILE_NAME = "audioFileName";
private MediaPlayer mediaPlayer;
private MediaController mediaController;
private String audioFile;
Database info = new Database(Player.this);
private Handler handler = new Handler();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
com.actionbarsherlock.app.ActionBar bar = getSupportActionBar();
Resources res = getResources();
bar.setBackgroundDrawable(res.getDrawable(R.drawable.top));
r1 = (RelativeLayout) findViewById(R.id.addview2);
r1.addView(adView);
wv = (WebView)findViewById(R.id.pl_wv);
wv.loadUrl("file:///android_asset/wave.gif");
done = (Button)findViewById(R.id.btn_done);
snz = (Button)findViewById(R.id.btn_snooze);
name = getIntent().getExtras().getString("flag");
audioFile = (Environment.getExternalStorageDirectory().getAbsolutePath() + "/com.pras.ToDo/" + name + ".3gpp");
Log.e("AUDIO FILE SOURCE ---->", audioFile);
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnPreparedListener(this);
mediaController = new MediaController(this) {
@Override
public void hide() {
mediaController.show(0);
}
};
try {
mediaPlayer.setDataSource(audioFile);
mediaPlayer.prepare();
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
Log.e("Sound-->", "stopped");
//pause();
wv.loadUrl("file:///android_asset/back.gif");
mediaPlayer.seekTo(0);
}
});
} catch (IOException e) {
Log.e(TAG, "Could not open file " + audioFile + " for playback.", e);
}
done.setClickable(true);
snz.setClickable(true);
done.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.e("done", "reminder deleted");
File file = new File(audioFile);
file.delete();
info.open();
info.deleteEntry(name);
info.deletealr(name);
info.close();
finish();
}
});
snz.setOnClickListener(new OnClickListener() {
@SuppressLint("SimpleDateFormat")
@Override
public void onClick(View v) {
Log.e("snooze", "reminder alarm");
String time = null;
info.open();
String key = info.getAlrData(name);
info.close();
SharedPreferences sp = getSharedPreferences("ESP", MODE_PRIVATE);
int i = sp.getInt("esp_time", 5);
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm a");
try {
Date k= sdf.parse(key);
Calendar c = Calendar.getInstance();
c.setTime(k);
c.add(Calendar.MINUTE, i);
time = sdf.format(c.getTime());
time = time.replace(" ", "");
time = time.replace(":", "");
time = time.replace("/", "");
Log.e("New Time---->", time);
info.open();
info.addEntry(time);
info.deleteEntry(name);
info.close();
} catch (ParseException e) {
Log.e("Date Parsing", "Date not parsed");
e.printStackTrace();
}
File from = new File(audioFile);
File to = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/com.pras.ToDo/"+ time+".3gpp");
from.renameTo(to);
finish();
}
});
}
@Override
protected void onStop() {
super.onStop();
mediaPlayer.stop();
mediaPlayer.release();
}
/* @Override
public boolean onTouchEvent(MotionEvent event) {
//the MediaController will hide after 3 seconds - tap the screen to make it appear again
mediaController.show();
return false;
}*/
//--MediaPlayerControl methods----------------------------------------------------
public void start() {
mediaPlayer.start();
wv.loadUrl("file:///android_asset/wave.gif");
}
public void pause() {
mediaPlayer.pause();
wv.loadUrl("file:///android_asset/back.gif");
}
public int getDuration() {
return mediaPlayer.getDuration();
}
public int getCurrentPosition() {
return mediaPlayer.getCurrentPosition();
}
public void seekTo(int i) {
mediaPlayer.seekTo(i);
}
public boolean isPlaying() {
return mediaPlayer.isPlaying();
}
public int getBufferPercentage() {
return 0;
}
public boolean canPause() {
return true;
}
public boolean canSeekBackward() {
return true;
}
public boolean canSeekForward() {
return true;
}
//--------------------------------------------------------------------------------
public void onPrepared(MediaPlayer mediaPlayer) {
Log.d(TAG, "onPrepared");
mediaController.setMediaPlayer(this);
mediaController.setAnchorView(findViewById(R.id.ivmc));
handler.post(new Runnable() {
public void run() {
mediaController.setEnabled(true);
mediaController.show();
}
});
}
@Override
public int getAudioSessionId() {
return 0;
}
@Override
public void onBackPressed(){
super.onBackPressed();
this.finish();
}
@Override
public void onPause() {
super.onPause();
Log.e("State", "pause");
}
@Override
public void onResume() {
super.onResume();
Log.e("State", "resume");
}
}
NotifierService.java
public class NotifierService extends Service {
Calendar c;
Date curdate;
Database info;
String date , time ;
int year, month, day, hour, min;
NotificationManager nm;
Notification mNotification;
Handler handler;
@SuppressLint("HandlerLeak")
@SuppressWarnings("deprecation")
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStart(intent, startId);
//Toast.makeText(this, "Service running", Toast.LENGTH_SHORT).show();
handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
}
};
new Thread(new Runnable(){
public void run() {
while(true)
{
try {
SharedPreferences sp = getSharedPreferences("SettingSharedPref", MODE_PRIVATE);
String check = sp.getString("settings", "asd");
Log.e("CheckBox notifier---->", check);
Thread.sleep(60000);
handler.sendEmptyMessage(0);
c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH)+1;
day = c.get(Calendar.DAY_OF_MONTH);
hour = c.get(Calendar.HOUR_OF_DAY);
min = c.get(Calendar.MINUTE);
info = new Database(NotifierService.this);
info.open();
String data = info.getTimeEntry(day, month , year, hour, min);
String res = info.setalr(c);
info.close();
if(data!= null){
Log.e("DATA FROM DB -->", data);
SharedPreferences shared = getSharedPreferences("TODOSHAREDPREF", MODE_PRIVATE);
if(data.equals(shared.getString("key", "asdf"))){
Log.e("Notifier---->", "Notification stopped");
}
else{
Editor editor = shared.edit();
editor.putString("key", data);
editor.commit();
if(check.equals("true")){
Log.e("check box notifier--->", "Notification stopped by checkbox");
}
else{
SharedPreferences sd_uri = getSharedPreferences("TONESHDPR", MODE_PRIVATE);
String s = sd_uri.getString("tone_set", "Default tone");
Uri sound;
if(s.equals("Default tone")){
sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}else{
sound =Uri.parse(s);
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext())
.setContentTitle("Voice Reminder")
.setSmallIcon(R.drawable.logo)
.setContentText("You have Something to do Listen up!")
.setSound(sound)
.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 })
.setLights(Color.RED, 3000, 3000)
.setAutoCancel(true)
.setOngoing(true);
Intent in = new Intent(getApplicationContext(), Player.class);
in.putExtra("flag", data);
PendingIntent pending=PendingIntent.getActivity(getApplicationContext(), 0, in ,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pending);
NotificationManager mNotificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
}
}
}
if(res!= null){
Log.e("DATA for Alarm -->", res);
SharedPreferences alarm = getSharedPreferences("ToDoALARM", MODE_PRIVATE);
if(res.equals(alarm.getString("alrKEY", "asdf"))){
Log.e("Notifier---->", "Notification stopped");
}
else{
Editor editor = alarm.edit();
editor.putString("alrKEY", res);
editor.commit();
if(check.equals("true")){
Log.e("check box notifier--->", "Notification stopped by checkbox");
}
else{
SharedPreferences sd_uri = getSharedPreferences("TONESHDPR", MODE_PRIVATE);
String s = sd_uri.getString("tone_set", "Default tone");
Uri sound;
if(s.equals("Default tone")){
sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}else{
sound =Uri.parse(s);
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext())
.setContentTitle("Voice Reminder Alarm")
.setSmallIcon(R.drawable.logo)
.setContentText("Listen up the notification you have snoozed earlier !")
.setSound(sound)
.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 })
.setLights(Color.RED, 3000, 3000)
.setAutoCancel(true)
.setOngoing(true);
Intent in = new Intent(getApplicationContext(), Player.class);
in.putExtra("flag", res);
PendingIntent pending=PendingIntent.getActivity(getApplicationContext(), 0, in ,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pending);
NotificationManager mNotificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
}
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
return Service.START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
player.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#FFFFFF"
android:weightSum="5"
android:id="@+id/main_audio_view" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="4"
android:background="#FFFFFF"
android:gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_snooze"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/snooze"
android:layout_marginLeft="40dp"
android:focusable="false"/>
<Button
android:id="@+id/btn_done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/done"
android:layout_marginLeft="70dp"
android:layout_marginRight="40dp"/>
</LinearLayout>
<WebView
android:id="@+id/pl_wv"
android:layout_width="300dp"
android:layout_height="150dp"
android:layout_marginTop="50dp"
android:layout_marginBottom="30dp"
android:background="#FFFFFF"/>
<ImageView
android:id="@+id/ivmc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:layout_marginBottom="30dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="bottom|center"
android:background="#FFFFFF" >
<RelativeLayout
android:id="@+id/addview2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="bottom" >
</RelativeLayout>
</LinearLayout>
</LinearLayout
什麼是錯誤您收到?張貼logcat。 –
隨意解釋一下「不工作」對你的意義。 – laalto
@Dev在logcat中沒有錯誤,當我點擊按鈕時什麼也沒有發生 –