標籤:
1 寫一個基類activity讓其他的繼承可以顯示的知道哪個活動 2 public class BaseActivity extends Activity { 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 // TODO Auto-generated method stub 6 super.onCreate(savedInstanceState); 7 Log.d("BaseActivity", getClass().getSimpleName()); 8 } 9 }10 11 public class MainActivity extends BaseActivity implements OnClickListener{12 private Button sendbtn;13 @Override14 protected void onCreate(Bundle savedInstanceState) {15 super.onCreate(savedInstanceState);16 setContentView(R.layout.activity_main);17 sendbtn=(Button)findViewById(R.id.send_notice);18 sendbtn.setOnClickListener(this);19 20 }21 22 @Override23 public boolean onCreateOptionsMenu(Menu menu) {24 // Inflate the menu; this adds items to the action bar if it is present.25 getMenuInflater().inflate(R.menu.main, menu);26 return true;27 }28 29 @Override30 public boolean onOptionsItemSelected(MenuItem item) {31 // Handle action bar item clicks here. The action bar will32 // automatically handle clicks on the Home/Up button, so long33 // as you specify a parent activity in AndroidManifest.xml.34 int id = item.getItemId();35 if (id == R.id.action_settings) {36 return true;37 }38 return super.onOptionsItemSelected(item);39 }40 41 @Override42 public void onClick(View v) {43 switch (v.getId()) {44 case R.id.send_notice:45 NotificationManager manger=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);46 Notification notification=new Notification(R.drawable.ic_launcher,"this is ticker text",System.currentTimeMillis());47 Intent intent=new Intent(this,SecondActivity.class);48 PendingIntent pi=PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);49 notification.setLatestEventInfo(this, "this is content title", "this is content text", pi);50 manger.notify(1, notification);51 break;52 53 default:54 break;55 }56 57 }58 }59 60 public class SecondActivity extends BaseActivity {61 @Override62 protected void onCreate(Bundle savedInstanceState) {63 super.onCreate(savedInstanceState);64 setContentView(R.layout.second);65 NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);66 manager.cancel(1);67 }68 }
安卓學習notification