日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android----使用代码 建立gprs 闹钟 连接

發(fā)布時(shí)間:2023/12/18 Android 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android----使用代码 建立gprs 闹钟 连接 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

?

Android:實(shí)用代碼(開啟啟動(dòng)、建立GPRS連接、鬧鐘等)

?

分類:?Android?312人閱讀?評(píng)論(0)?收藏?舉報(bào) androidactionstringservice手機(jī)j2me

1:查看是否有存儲(chǔ)卡插入String status=Environment.getExternalStorageState();
if(status.equals(Enviroment.MEDIA_MOUNTED))
{
? ?說明有SD卡插入
}

2:讓某個(gè)Activity透明?OnCreate中不設(shè)Layoutthis.setTheme(R.style.Theme_Transparent);以下是Theme_Transparent的定義(注意transparent_bg是一副透明的圖片)
3:在屏幕元素中設(shè)置句柄
使用Activity.findViewById來取得屏幕上的元素的句柄. 使用該句柄您可以設(shè)置或獲取任何該對(duì)象外露的值.
TextView msgTextView = (TextView)findViewById(R.id.msg);
? ?msgTextView.setText(R.string.push_me);?
4:發(fā)送短信? ?? ?? ?? ?String body=”this is mms demo”;? ?? ?? ???Intent mmsintent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(”smsto”, number, null));
? ?? ?? ???mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_BODY, body);
? ?? ?? ???mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_COMPOSE_MODE, true);
? ?? ?? ???mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_EXIT_ON_SENT, true);
? ?? ?? ?? ?startActivity(mmsintent);?
5:發(fā)送彩信? ?? ?? ???StringBuilder sb = new StringBuilder();? ?? ?? ?? ?sb.append(”file://”);? ?? ?? ?? ?sb.append(fd.getAbsoluteFile());? ?? ?? ?? ?Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(”mmsto”, number, null));
? ?? ?? ?? ?// Below extra datas are all optional.
? ?? ?? ?? ?intent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_SUBJECT, subject);
? ?? ?? ?? ?intent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_BODY, body);
? ?? ?? ?? ?intent.putExtra(Messaging.KEY_ACTION_SENDTO_CONTENT_URI, sb.toString());
? ?? ?? ?? ?intent.putExtra(Messaging.KEY_ACTION_SENDTO_COMPOSE_MODE, composeMode);
? ?? ?? ?? ?intent.putExtra(Messaging.KEY_ACTION_SENDTO_EXIT_ON_SENT, exitOnSent);? ?? ?? ?? ?startActivity(intent);
6:發(fā)送Mail? ?? ?? ?? ? mime = “img/jpg”;
? ?? ?? ?? ?shareIntent.setDataAndType(Uri.fromFile(fd), mime);
? ?? ?? ?? ?shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(fd));
? ?? ?? ?? ?shareIntent.putExtra(Intent.EXTRA_SUBJECT, subject);? ?? ?? ?? ?shareIntent.putExtra(Intent.EXTRA_TEXT, body);
7:注冊(cè)一個(gè)BroadcastReceiverregisterReceiver(mMasterResetReciever, new IntentFilter(”O(jiān)MS.action.MASTERRESET”));private BroadcastReceiver mMasterResetReciever = new BroadcastReceiver() {? ?? ???public void onReceive(Context context, Intent intent){
? ?? ?? ?? ?String action = intent.getAction();
? ?? ?? ?? ?if(”oms.action.MASTERRESET”.equals(action)){
? ?? ?? ?? ?? ? RecoverDefaultConfig();
? ?? ?? ?? ?}
? ?? ???}? ? };?
8:定義ContentObserver,監(jiān)聽某個(gè)數(shù)據(jù)表

private ContentObserver mDownloadsObserver = new DownloadsChangeObserver(Downloads.CONTENT_URI);private class DownloadsChangeObserver extends ContentObserver {
? ?? ???public DownloadsChangeObserver(Uri uri) {
? ?? ?? ?? ?super(new Handler());? ?? ???}? ?? ???@Override
? ?? ???public void onChange(boolean selfChange) {}??
? ?? ???}

9:獲得 手機(jī)UA?public String getUserAgent()
? ? {
? ?? ?? ???String user_agent = ProductProperties.get(ProductProperties.USER_AGENT_KEY, null);
? ?? ?? ?? ?return user_agent;
? ? }?

10:清空手機(jī)上Cookie?CookieSyncManager.createInstance(getApplicationContext());
? ?? ???CookieManager.getInstance().removeAllCookie();?

11:建立GPRS連接?? ?//Dial the GPRS link.
? ? private boolean openDataConnection() {
? ?? ???// Set up data connection.
? ?? ???DataConnection conn = DataConnection.getInstance();? ?? ?? ?? ?? ???if (connectMode == 0) {
? ?? ?? ?? ?? ? ret = conn.openConnection(mContext, “cmwap”, “cmwap”, “cmwap”);
? ?? ?? ?? ?} else {
? ?? ?? ?? ?? ? ret = conn.openConnection(mContext, “cmnet”, “”, “”);
? ?? ?? ?? ?}? ? }?

12:PreferenceActivity 用法
public class Setting extends PreferenceActivity{? ? public void onCreate(Bundle savedInstanceState) {
? ?? ???super.onCreate(savedInstanceState);
? ?? ???addPreferencesFromResource(R.xml.settings);
? ? }}Setting.xml:? ?? ?? ?? ?Android:key=”seting2″
? ?? ?? ?? ?android:title=”@string/seting2″
? ?? ?? ?? ?android:summary=”@string/seting2″/>? ?? ?? ?? ?android:key=”seting1″
? ?? ?? ?? ?android:title=”@string/seting1″
? ?? ?? ?? ?android:summaryOff=”@string/seting1summaryOff”
? ?? ?? ?? ?android:summaryOn=”@stringseting1summaryOff”/>?

13:通過HttpClient從指定server獲取數(shù)據(jù)?? ?? ?? ?? ? DefaultHttpClient httpClient = new DefaultHttpClient();
? ?? ?? ?? ?HttpGet method = new HttpGet(“http://www.baidu.com/1.html”);
? ?? ?? ?? ?HttpResponse resp;
? ?? ?? ?? ?Reader reader = null;
? ?? ?? ?? ?try {
? ?? ?? ?? ?? ? // AllClientPNames.TIMEOUT
? ?? ?? ?? ?? ? HttpParams params = new BasicHttpParams();
? ?? ?? ?? ?? ? params.setIntParameter(AllClientPNames.CONNECTION_TIMEOUT, 10000);
? ?? ?? ?? ?? ? httpClient.setParams(params);
? ?? ?? ?? ?? ? resp = httpClient.execute(method);
? ?? ?? ?? ?? ? int status = resp.getStatusLine().getStatusCode();? ?? ?? ?? ?? ? if (status != HttpStatus.SC_OK) return false;? ?? ?? ?? ?? ? // HttpStatus.SC_OK;
? ?? ?? ?? ?? ? return true;
? ?? ?? ?? ?} catch (ClientProtocolException e) {
? ?? ?? ?? ?? ? // TODO Auto-generated catch block
? ?? ?? ?? ?? ? e.printStackTrace();
? ?? ?? ?? ?} catch (IOException e) {
? ?? ?? ?? ?? ? // TODO Auto-generated catch block
? ?? ?? ?? ?? ? e.printStackTrace();
? ?? ?? ?? ?} finally {
? ?? ?? ?? ?? ? if (reader != null) try {
? ?? ?? ?? ?? ?? ???reader.close();
? ?? ?? ?? ?? ? } catch (IOException e) {
? ?? ?? ?? ?? ?? ???// TODO Auto-generated catch block
? ?? ?? ?? ?? ?? ???e.printStackTrace();
? ?? ?? ?? ?? ? }
? ?? ?? ?? ?}?
14:顯示toast
Toast.makeText(this._getApplicationContext(), R.string._item, Toast.LENGTH_SHORT).show();??

15:屏幕顯示

程序中默的顯示是帶有標(biāo)題欄和系統(tǒng)信息欄的,有的時(shí)候,這很影響程序界面的美觀。手機(jī)默認(rèn)的是豎屏,或與感應(yīng)器狀態(tài)相關(guān),為了某種效果,我們的程序需要限制使用橫屏或豎屏。以下的代碼就解決了上述問題。
//設(shè)置為無標(biāo)題欄?
requestWindowFeature(Window.FEATURE_NO_TITLE);?
//設(shè)置為全屏模式?
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//設(shè)置為橫屏?

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

16:Intent傳參?當(dāng)Activity與Activity/Service(或其它情況)有時(shí)與要進(jìn)行參數(shù)傳遞,最常用也是最簡單的方式就是通過Intent來處理。

看如下代碼:?
Intent intent = new Intent(...);?
Bundle bundle = new Bundle();?
bundle.putString("NAME", "zixuan");?
intent.putExtras(bundle);?
context.startActivity(intent); 或 context.startService(intent);?

當(dāng)然,有傳送就有接收,接收也很簡單,如:?
Bundle bunde = intent.getExtras();?
String name = bunde.getInt("NAME");?

當(dāng)然參數(shù)KEY要與傳送時(shí)的參數(shù)一致。

17:獲取手機(jī)號(hào)?在j2me中,根本沒有辦法獲取用戶的手機(jī)號(hào)碼,就連獲取手機(jī)串號(hào)(IMEI)都基本上無法實(shí)現(xiàn),然后在android手機(jī)上一切都是如此的簡單,看代碼:

TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
String imei = tm.getDeviceId();?
String tel = tm.getLine1Number();?

看來,android的確加速了j2me的消亡。

18:振動(dòng)器?總感覺手機(jī)上的振動(dòng)器沒有多大用處(當(dāng)然靜音模式下的振鈴很有用),但還是順帶著說一下吧,只有兩行代碼:

1、獲取振動(dòng)服務(wù)的實(shí)例?
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);?
2、設(shè)置振動(dòng)時(shí)長,單位當(dāng)然也是ms?
vibrator.vibrate(1000);?
如果你覺得這樣過去單調(diào)的話,可以設(shè)個(gè)節(jié)奏:?
vibrator.vibrate(new long[]{10, 100, 20, 200}, -1);?
兩個(gè)參數(shù),習(xí)慣告訴我第一個(gè)是節(jié)奏,第二個(gè)是重復(fù)次數(shù),可事實(shí)并沒有這么簡單,我翻譯不好,大家還是看原文吧:?
public void vibrate (long[] pattern, int repeat)?
pattern: an array of longs of times to turn the vibrator on or off.?
repeat: the index into pattern at which to repeat, or -1 if you don't want to repeat.

google喜歡弄些技巧,我卻覺得這里有點(diǎn)弄巧成拙了。??

19:鬧鐘管理?最近看了一下Android的鬧鐘管理類(AlarmManager),真不錯(cuò)誤,強(qiáng)大又簡單,代碼如下:


1、建立一個(gè)AlarmReceiver繼承入BroadcastReceiver,并在AndroidManifest.xml聲明?
  • public static class AlarmReceiver extends BroadcastReceiver {?
  • @Override?
  • public void onReceive(Context context, Intent intent) {?
  • Toast.makeText(context, "鬧鐘提示:時(shí)間到!", Toast.LENGTH_LONG).show();?
  • }?
  • }
  • 復(fù)制代碼

    2、建立Intent和PendingIntent,來調(diào)用目標(biāo)組件。?
  • Intent intent = new Intent(this, AlarmReceiver.class);?
  • PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
  • 復(fù)制代碼

    3、設(shè)置鬧鐘?
    獲取鬧鐘管理的實(shí)例:?
  • AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
  • 復(fù)制代碼
    設(shè)置單次鬧鐘:?
  • alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (5*1000), pendingIntent);
  • 復(fù)制代碼
    設(shè)置周期鬧鐘:?
  • alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (10*1000), (24*60*60*1000), pendingIntent);
  • 復(fù)制代碼

    20:開機(jī)自啟動(dòng)1.定義一個(gè)BroadcastReceiver
  • public class BootReceiver extends BroadcastReceiver {
  • ? ? ? ? public void onReceive(Context ctx, Intent intent) {
  • ? ? ? ? ? ? ? ? Log.d("BootReceiver", "system boot completed");
  • ? ? ? ? ? ? ? ? //start activity
  • ? ? ? ? ? ? ? ? String action="android.intent.action.MAIN";
  • ? ? ? ? ? ? ? ? String category="android.intent.category.LAUNCHER";
  • ? ? ? ? ? ? ? ? Intent myi=new Intent(ctx,CustomDialog.class);
  • ? ? ? ? ? ? ? ? myi.setAction(action);
  • ? ? ? ? ? ? ? ? myi.addCategory(category);
  • ? ? ? ? ? ? ? ? myi.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  • ? ? ? ? ? ? ? ? ctx.startActivity(myi);
  • ? ? ? ? ? ? ? ? //start service
  • ? ? ? ? ? ? ? ? Intent s=new Intent(ctx,MyService.class);
  • ? ? ? ? ? ? ? ? ctx.startService(s);
  • ? ? ? ? ? ? ? ? }
  • ? ? ? ? }
  • 復(fù)制代碼

    2.配置Receiver的許可,允許接收系統(tǒng)啟動(dòng)消息,在AndroidManifest.xml中:
  • ? ? ? ? <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>??
  • 復(fù)制代碼

    3.配置Receiver,可以接收系統(tǒng)啟動(dòng)消息,在AndroidManifest.xml中
  • ? ? <receiver android:name=".app.BootReceiver">
  • ? ?? ???? ? ? ? ? ? ? ? <intent-filter>
  • ? ?? ???? ? ? ? ? ? ? ? ? ? ? ? <action android:name="android.intent.action.BOOT_COMPLETED"/>
  • ? ?? ?? ?? ???? ? ? ? ? ? ? ? ? ? ? ?? ?<category android:name="android.intent.category.HOME" />
  • ? ?? ???? ? ? ? ? ? ? ? </intent-filter>
  • ? ?? ? ? ? ? ? ? ? ? ???</receiver>
  • 復(fù)制代碼

    轉(zhuǎn)載于:https://www.cnblogs.com/zhengbeibei/archive/2013/03/02/2940104.html

    總結(jié)

    以上是生活随笔為你收集整理的Android----使用代码 建立gprs 闹钟 连接的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。