編輯:關於Android編程
與Service相似,在android中還有一種運行在後台的4大組件之一,他就是broadCcast,但是他不用考慮是否有接受的對象,只需要考慮發送即可。
要想使用BroadCast我們可以使用兩種方法
第一:將broadCcast配置到配置文件當中。
第二:將broadCcast與class文件進行綁定。
下面我們分別來進行說明。
廣播類
package com.example.broadcast; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class BroadCastUtil extends BroadcastReceiver { public void BroadcastReceiver(){ System.out.println("**一個廣播事件被建立"); } @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub Toast.makeText(context, "廣播已經啟動", 2).show(); } }主文件
package com.example.broadcast; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.activity_main); button = (Button) this.findViewById(R.id.button1); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub // 設置過濾 Intent intent = new Intent(Intent.ACTION_EDIT); // 開啟廣播 MainActivity.this.sendBroadcast(intent); } }); } }
在 application節點下增加 配置
使用registerReceiver(BroadcastReceiver, IntentFilter)進行綁定過濾操作
其他的不用進行改變只用改變
主文件 就OK<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PC9wPgo8cHJlIGNsYXNzPQ=="brush:java;">package com.example.broadcast;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
button = (Button) this.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 設置過濾
Intent intent = new Intent("com.example.broadcast");
IntentFilter filter=new IntentFilter("com.example.broadcast");
BroadCastUtil broadCastUtil=new BroadCastUtil();
MainActivity.this.registerReceiver(broadCastUtil, filter);
// 開啟廣播
MainActivity.this.sendBroadcast(intent);
}
});
}
}
這樣進行綁定和配置的話,配置文件中就不需要配置信息了 。
使用廣播啟動Service
Service
package com.example.broadecast2; import android.app.Service; import android.content.Intent; import android.os.IBinder; public class MyService extends Service { @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { // TODO Auto-generated method stub System.out.println("** SERVICE onCreate"); super.onCreate(); } @Override public void onDestroy() { // TODO Auto-generated method stub System.out.println("** SERVICE onDestroy"); super.onDestroy(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub System.out.println("** SERVICE onStartCommand"); return Service.START_CONTINUATION_MASK; } }BroadCask
package com.example.broadecast2; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class MyBroadeCast extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent1) { context.startService(new Intent(context,MyService.class)) ; } }
package com.example.broadecast2; import android.os.Bundle; import android.app.Activity; import android.app.PendingIntent; import android.content.Intent; import android.view.Menu; //使用 廣播啟動服務 public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent =new Intent("com.example.broadecast2.DEMO"); MainActivity.this.sendBroadcast(intent); } }
使用廣播進行鬧鐘的控制
對話框
package com.example.broadcast3; import java.text.SimpleDateFormat; import java.util.Date; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; public class AlertMessage extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); new AlertDialog.Builder(this) .setIcon(R.drawable.b9) .setTitle("鬧鐘時間到") .setMessage( "當前時間為:" + new SimpleDateFormat( "yyyy年MM月dd日 HH時 mm分 ss秒") .format(new Date())) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub AlertMessage.this.finish(); } }).create().show(); } }
package com.example.broadcast3; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class MyBroadCast extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub Intent it = new Intent(context, AlertMessage.class); it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(it); } }Main
package com.example.broadcast3; import java.util.Calendar; import android.os.Bundle; import android.R.integer; import android.app.Activity; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.TimePicker; import android.widget.TimePicker.OnTimeChangedListener; import android.widget.Toast; public class MainActivity extends Activity { private Button set,delete; private TextView info; private TimePicker time; private int hour ; private int minute; private Calendar calendar = Calendar.getInstance() ; private AlarmManager alarm; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); set=(Button)this.findViewById(R.id.button1); delete=(Button)this.findViewById(R.id.button2); info=(TextView)this.findViewById(R.id.textView1); time=(TimePicker)this.findViewById(R.id.timePicker1); time.setIs24HourView(true); this.alarm = (AlarmManager) super.getSystemService(Context.ALARM_SERVICE) ; time.setOnTimeChangedListener(new OnTimeChangedListener() { @Override public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { // TODO Auto-generated method stub MainActivity.this.hour=hourOfDay; MainActivity.this.minute=minute; MainActivity.this.calendar.setTimeInMillis(System.currentTimeMillis()) ; MainActivity.this.calendar.set(Calendar.HOUR_OF_DAY, hourOfDay) ; MainActivity.this.calendar.set(Calendar.MINUTE, minute) ; MainActivity.this.calendar.set(Calendar.SECOND, 0) ; MainActivity.this.calendar.set(Calendar.MILLISECOND, 0) ; } }); set.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent =new Intent(MainActivity.this, AlertMessage.class); intent.setAction("com.example.broadcast3"); PendingIntent sender=PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); MainActivity.this.alarm.set(AlarmManager.RTC_WAKEUP, MainActivity.this.calendar.getTimeInMillis(), sender); MainActivity.this.info.setText("鬧鐘響起的時間是:" + MainActivity.this.hour + "時" + MainActivity.this.minute + "分。"); Toast.makeText(MainActivity.this, "鬧鐘設置成功!", Toast.LENGTH_LONG).show(); } }); delete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if (MainActivity.this.alarm != null) { Intent intent = new Intent(MainActivity.this, MyBroadCast.class); intent.setAction("org.lxh.action.setalarm") ; PendingIntent sender = PendingIntent.getBroadcast( MainActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); MainActivity.this.alarm.cancel(sender) ; // 取消 MainActivity.this.info.setText("當前沒有設置鬧鐘。") ; Toast.makeText(MainActivity.this, "鬧鐘刪除成功!", Toast.LENGTH_LONG).show(); } } }); } }
下節預報:android基本繪圖
Android 中與 Touch 事件相關的方法包括:dispatchTouchEvent(MotionEvent ev)、onInterceptTouchEv
JNI層方法命名規范在java層對應的native方法的聲明,test方法是一個本地方法,其參數是字符串類型,返回值是字符串類型。package com.kltz88.j
綜述對於MVP (Model View Presenter)架構是從著名的MVC(Model View Controller)架構演變而來的。而對於Android應用的開
前言本文在Ubuntu 64位系統上對qemu項目進行交叉編譯,並且只編譯與qemu user mode有關的代碼。下文中的”NDK”若無特殊說明