編輯:關於Android編程
第一行代碼Android學習:第九部分主要涉及到通知、接收和發送短信、調用攝像頭和相冊
1.通知DYHDM_08_00NotificationTest
MainActivity.javapackage com.example.dyhdm_08_00notificationtest; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; /** * TODO 發送通知 * @author 張志安 * @date: 2016-8-16 下午5:42:40 */ public class MainActivity extends Activity { private Button bt_notify; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt_notify = (Button) findViewById(R.id.bt_notify); bt_notify.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 獲取NotificationManager實例 NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //創建一個Notification對象,存儲通知所需要的各種信息 Notification notification = new Notification( R.drawable.ic_launcher, "hello zza", System .currentTimeMillis()); // 構建一個PendingIntentc傳入setLatestEventInfo()的第四個參數,可以傳null Intent intent = new Intent(MainActivity.this, SecondActivity.class); PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); // 對通知的布局進行設定 notification.setLatestEventInfo(MainActivity.this, "Zza", "Hello", pi); manager.notify(1, notification); } }); } }SecondActivity.java
package com.example.dyhdm_08_00notificationtest; import android.os.Bundle; import android.app.Activity; import android.app.NotificationManager; import android.view.Menu; public class SecondActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); //取消通知 NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); manager.cancel(1); } }
2.接收和發送短信DYHDM_08_01SMSTest
activity_main.xml
MainActivity.java
package com.example.dyhdm_08_01smstest; import android.app.Activity; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.IntentSender.SendIntentException; import android.os.Bundle; import android.telephony.gsm.SmsManager; import android.telephony.gsm.SmsMessage; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private TextView sender; private TextView content; private IntentFilter receiveFilter; private MessageReceiver messageReceiver; private EditText to; private EditText msgInput; private Button send; private IntentFilter sendFilter; private SendStatusReceiver sendStatusReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sender = (TextView) findViewById(R.id.tv_send); content = (TextView) findViewById(R.id.tv_content); // 接收短信的廣播接收者 // 動態注冊廣播接收器 receiveFilter = new IntentFilter(); receiveFilter.addAction("android.provider.Telephony.SMS_RECEIVED"); // receiveFilter.setPriority(100); messageReceiver = new MessageReceiver(); registerReceiver(messageReceiver, receiveFilter); // 發送短信的廣播接收者 sendFilter = new IntentFilter(); sendFilter.addAction("SENT_SMS_ACTION"); sendStatusReceiver = new SendStatusReceiver(); registerReceiver(sendStatusReceiver, sendFilter); // 發送短信 to = (EditText) findViewById(R.id.et_to); msgInput = (EditText) findViewById(R.id.et_msg); send = (Button) findViewById(R.id.bt_send); send.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { SmsManager smsManager = SmsManager.getDefault(); Intent sendIntent = new Intent("SENT_SMS_ACTION"); PendingIntent pi = PendingIntent.getBroadcast( MainActivity.this, 0, sendIntent, 0); smsManager.sendTextMessage(to.getText().toString(), null, msgInput.getText().toString(), pi, null); } }); } // 接收信息的廣播接收者 class MessageReceiver extends BroadcastReceiver { /** * 重載方法 */ @Override public void onReceive(Context context, Intent intent) { Bundle bundle = intent.getExtras(); Object[] pdus = (Object[]) bundle.get("pdus");// 提取短信消息 SmsMessage[] messages = new SmsMessage[pdus.length]; for (int i = 0; i < messages.length; i++) { messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]); String address = messages[0].getOriginatingAddress();// 獲取發送方號碼 String fullMessage = ""; for (SmsMessage smsMessage : messages) { fullMessage += smsMessage.getMessageBody();// 獲取短信內容 } sender.setText(address); content.setText(fullMessage); // 可攔截系統收到短信 // abortBroadcast(); } } } // 接收短信發送 class SendStatusReceiver extends BroadcastReceiver { /** * 重載方法 */ @Override public void onReceive(Context context, Intent intent) { if (getResultCode() == RESULT_OK) { Toast.makeText(context, "發送成功", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(context, "發送失敗", Toast.LENGTH_SHORT).show(); } } } /** * 重載方法 */ @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(messageReceiver); unregisterReceiver(sendStatusReceiver); } }
3.調用攝像頭和相冊DYHDM_08_02ChoosePicTest
activity_main.xml
MainActivity.java
package com.example.dyhdm_08_02choosepictest; import java.io.File; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Picture; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity { public static final int TAKE_PHONE = 1; public static final int CROP_PHONE = 2; private Button bt_take_phone; private Button bt_choose_phone; private ImageView iv_phone; private Uri imageUri; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt_take_phone = (Button) findViewById(R.id.bt_take_phone); iv_phone = (ImageView) findViewById(R.id.iv_picture); bt_take_phone.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 創建File對象,存儲拍下的圖片 // 存儲在手機的sd卡根目錄下 // Environment.getExternalStorageDirectory()獲取sd卡的根目錄 File outputImage = new File(Environment .getExternalStorageDirectory(), "temp.jpg"); try { if (outputImage.exists()) { outputImage.delete(); } outputImage.createNewFile(); } catch (Exception e) { e.printStackTrace(); } // Uri.fromFile()將File對象轉換為Uri對象 imageUri = Uri.fromFile(outputImage); // 拍照 Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(intent, TAKE_PHONE); } }); bt_choose_phone = (Button) findViewById(R.id.bt_choose_phone); bt_choose_phone.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { File outputImage = new File(Environment .getExternalStorageDirectory(), "output_image.jpg"); try { if (outputImage.exists()) { outputImage.delete(); } outputImage.createNewFile(); } catch (Exception e) { e.printStackTrace(); } imageUri = Uri.fromFile(outputImage); // 拍照 有問題 華為點擊照片後無法獲取,三星好用 Intent intent = new Intent("android.intent.action.GET_CONTENT"); intent.setType("image/*"); intent.putExtra("crop", true); intent.putExtra("scale", true); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(intent, CROP_PHONE); } }); } /** * 重載方法 */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case TAKE_PHONE: if (resultCode == RESULT_OK) { // 裁剪 Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(imageUri, "image/*"); intent.putExtra("scale", true); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(intent, CROP_PHONE); } break; case CROP_PHONE: if (resultCode == RESULT_OK) { try { Bitmap bitmap = BitmapFactory .decodeStream(getContentResolver().openInputStream( imageUri)); iv_phone.setImageBitmap(bitmap); } catch (Exception e) { e.printStackTrace(); } } break; default: break; } } }
1.生命周期場景演示 : 切換到該Fragment11-29 14:26:35.095: D/AppListFragment(7649): onAttach11-29 1
在之前Google的系統還沒有更新的時候可以通過修改GoAgent的proxy.ini來登陸美國Play。現在介Google Play美國登錄的辦法,但是G
需要改變下拉狀態欄的背景色。關於這方面的代碼路徑 frameworks/base/packages/SystemUI中通過androidSDK自帶的工具uiautomat
MyRecorder(仿微信,錄制音頻並發送功能)①布局實現(activity_main.xml) 布局采用線性布局,上面使用的一個ListView,下面使用的是一個自定