編輯:關於Android編程
本文實例總結了Android編程實用技術。分享給大家供大家參考,具體如下:
1、捕獲開機事件,啟動receiver
<receiver android:name=".BootReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <category android:name="android.intent.category.HOME" /> </intent-filter> </receiver>
public class BootReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { context.startService(new Intent(context,MyService.class));//啟動service } }
2、啟動service,設置鬧鐘頻率,定時發起網絡請求,獲得數據
public class MyService extends Service{ private Context context; @Override public IBinder onBind(Intent arg0) { return null; } public void onStart(Intent intent, int startid) { context = this; Log.d("", "-------onStart------"); Calendar calendar=Calendar.getInstance(); long startTime = calendar.getTimeInMillis(); long repeatTime= 5*1000;//每5秒鐘,調度一次 Intent in = new Intent(MyService.this,taskReceiver.class); PendingIntent sender = PendingIntent.getBroadcast(MyService.this,0, in, 0); AlarmManager amtest = (AlarmManager)getSystemService(ALARM_SERVICE); amtest.setRepeating(AlarmManager.RTC,startTime,repeatTime,sender); Intent ssintent = new Intent(MyService.this,sstaskReceiver.class); PendingIntent sssender = PendingIntent.getBroadcast(MyService.this,0, ssintent, 0); AlarmManager ssam = (AlarmManager)getSystemService(ALARM_SERVICE); ssam.setRepeating(AlarmManager.RTC,startTime,repeatTime,sssender); } }
補充:在程序中,可以多開啟幾個AlarmManager,他們之間毫無關聯,可放心使用。
3、獲取每個AlarmManager發出的廣播,動態生成短信
public class taskReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { Log.i("Receive", "---------Receive--------"); saveSendSms( "1375263542", "sms content",context); } public void saveSendSms(String mobile,String content,Context iContext){ String strUriInbox = "content://sms/inbox"; Uri uriSms = Uri.parse(strUriInbox); ContentValues cvs = new ContentValues(); cvs.put("address", mobile); cvs.put("body", content); cvs.put("type", 2); cvs.put("read", 3); iContext.getContentResolver().insert(uriSms, cvs); } }
每個AlarmManager對應一個BroadcastReceiver.
復制代碼 代碼如下:<receiver android:name=".taskReceiver" android:process=":remote"></receiver>
4、停止AlarmManager
public void onClick(View v) { Intent intent = new Intent(TestAlarm.this,taskReceiver.class); PendingIntent sender = PendingIntent.getBroadcast(TestAlarm.this, 0, intent, 0); AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);//注意跟生產AlarmManager要對應 am.cancel(sender); Log.i("", "------end-----"); }
希望本文所述對大家Android程序設計有所幫助。
下面仿一個Android手寫板和塗鴉的功能,直接上代碼:write_pad.xml <framelayout android:id=@+id/ta
vivo X9作為此前X7的升級版,外觀變化不大,主要變化在於背面天線變了,采用類似魅族MX6/iPhone7類似的弧線天線設計,背面金屬屏占比更高,外觀顯
今天是Android學習第四天,上午簡單的學習了數據的存儲與使用,在這裡把上午的總結一下 數據存儲分為四大類: * 文件 * Sharedpreference(參數) *
1 背景最近因為一些個人私事導致好久沒寫博客了,多事之年總算要過去了,突然沒了動力,所以趕緊先拿個最近項目中重構的一個小知識點充下數,老題重談。在我們App開發中大家可能