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);
}
}