您好,登錄后才能下訂單哦!
package com.example.customnotification;
import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.RemoteViews;
import android.widget.Toast;
public class MainActivity extends Activity
{
private NotificationManager manager;
private Notification mNotify;
private RemoteViews remoteViews;
private MyBroadCast receiver;
private static String ACTION="a";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// 注冊廣播,并設置過濾條件
receiver = new MyBroadCast();
IntentFilter filter=new IntentFilter("a");
registerReceiver(receiver, filter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@SuppressWarnings("deprecation")
public void notify(View v){
if(mNotify==null){
mNotify = new Notification(R.drawable.ic_launcher, "酷狗音樂",
System.currentTimeMillis());
Intent intent = new Intent(this, SecondActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,123,
intent, PendingIntent.FLAG_UPDATE_CURRENT);//默認整個布局的點擊事件
//自定布局 notify_music.xml
remoteViews = new RemoteViews(getPackageName(), R.layout.notify_music);
Intent action=new Intent("a");
remoteViews.setOnClickPendingIntent(R.id.notify_music_play_imgbtn,
PendingIntent.getBroadcast(MainActivity.this, 11, action,PendingIntent.FLAG_UPDATE_CURRENT)); //播放按鈕的點擊事件
mNotify.contentView=remoteViews; // 實現自定義布局和 布局事件
mNotify.contentIntent=pendingIntent;
}
manager.notify(0, mNotify);
}
class MyBroadCast extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(MainActivity.this, "11111111111111", Toast.LENGTH_LONG).show();
Log.e("我的官博", "按鈕點擊事件");
startActivity(new Intent(MainActivity.this,ThirdActivity.class));
}
}
@Override
protected void onStop()
{
// TODO Auto-generated method stub
super.onStop();
unregisterReceiver(receiver);
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。