发表于: 2018-11-25 22:16:00

0 904


一、今天完成的事情

使用隐式Intent拨打电话有两种方式,一种是跳转到拨号界面,但不拨打,需要手动点击拨打,另一种是直接拨打电话

AndroidManifest中

<activity
       android:name=".activity.CallActivity"
       android:label="电话">
       <intent-filter>
           <action android:name="android.intent.action.VIEW" />
           <category android:name="android.intent.category.DEFAULT" />
           <data android:scheme="tel" />
           <data android:scheme="smsto" />
       </intent-filter>
   </activity>
</application>

第一种,跳转到拨号界面

Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:" + phoneNumberET.getText()));
startActivity(intent);

第二种,直接拨打

Intent intent2 = new Intent(Intent.ACTION_CALL);
intent2.setData(Uri.parse("tel:" + phoneNumberET.getText()));
startActivity(intent2);

其中,第二种方法需要在代码中申请权限

/*
申请权限:电话权限、短信权限
*/
public void requestPermission() {
   List<String> permissionList = new ArrayList<String>();

   if (ContextCompat.checkSelfPermission(CallActivity.this,
           Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
       permissionList.add(Manifest.permission.CALL_PHONE);
   }
   if(ContextCompat.checkSelfPermission(CallActivity.this,
           Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
       permissionList.add(Manifest.permission.SEND_SMS);
   }

   if (permissionList.isEmpty()) {
       //
   } else {
       String[] permissions = permissionList.toArray(new String[permissionList.size()]);
       ActivityCompat.requestPermissions(CallActivity.this, permissions, 1);
   }
}

@Override
public void onRequestPermissionsResult (int requestCode, String[] permissions, int[] grantResult) {
   switch (requestCode) {
       case 1:
           if (grantResult.length > 0) {
               for (int result : grantResult) {
                   if (result != PackageManager.PERMISSION_GRANTED) {
                       Toast.makeText(CallActivity.this, "请授予权限", Toast.LENGTH_SHORT).show();
                       finish();
                       return;
                   }
               }
           } else {
               Toast.makeText(CallActivity.this, "未知错误", Toast.LENGTH_SHORT).show();
               finish();
           }
           break;
       default:
           break;
   }
}


解析嵌套的JSON数据

{"code":0,"articleList":{"records":[{"id":23,"createAt":null,"updateAt":1540990298514,"createBy":null,"updateBy":null,"author":"testAuthor","body":null,"type":0,"cover":"testCover","digest":"testDigest","title":"testTitle","likeAmount":0,"collectionAmount":0,"status":null},{"id":30,"createAt":null,"updateAt":1540989836094,"createBy":null,"updateBy":null,"author":"testAuthor","body":null,"type":0,"cover":"testCover","digest":"testDigest","title":"testTitle","likeAmount":0,"collectionAmount":0,"status":null},{"id":29,"createAt":null,"updateAt":1540989790676,"createBy":null,"updateBy":null,"author":"testAuthor","body":null,"type":0,"cover":"testCover","digest":"testDigest","title":"testTitle","likeAmount":0,"collectionAmount":0,"status":null},{"id":28,"createAt":null,"updateAt":1540989748445,"createBy":null,"updateBy":null,"author":"testAuthor","body":null,"type":0,"cover":"testCover","digest":"testDigest","title":"testTitle","likeAmount":0,"collectionAmount":0,"status":null},{"id":26,"createAt":null,"updateAt":1540988881075,"createBy":null,"updateBy":null,"author":"testAuthor","body":null,"type":0,"cover":"testCover","digest":"testDigest","title":"testTitle","likeAmount":0,"collectionAmount":0,"status":null},{"id":25,"createAt":null,"updateAt":1540959878473,"createBy":null,"updateBy":null,"author":"testAuthor","body":null,"type":0,"cover":"testCover","digest":"testDigest","title":"testTitle","likeAmount":0,"collectionAmount":0,"status":null},{"id":24,"createAt":null,"updateAt":1540902013843,"createBy":null,"updateBy":null,"author":"testAuthor","body":null,"type":0,"cover":"testCover","digest":"testDigest","title":"testTitle","likeAmount":0,"collectionAmount":0,"status":null},{"id":22,"createAt":null,"updateAt":1540902013720,"createBy":null,"updateBy":null,"author":"testAuthor","body":null,"type":0,"cover":"testCover","digest":"testDigest","title":"testTitle","likeAmount":0,"collectionAmount":0,"status":null},{"id":20,"createAt":null,"updateAt":1540902013597,"createBy":null,"updateBy":null,"author":"testAuthor","body":null,"type":0,"cover":"testCover","digest":"testDigest","title":"testTitle","likeAmount":0,"collectionAmount":0,"status":null},{"id":18,"createAt":null,"updateAt":1540902013468,"createBy":null,"updateBy":null,"author":"testAuthor","body":null,"type":0,"cover":"testCover","digest":"testDigest","title":"testTitle","likeAmount":0,"collectionAmount":0,"status":null}],"total":30,"size":10,"pages":3,"current":1,"previous":0,"next":2},"message":"操作成功"}

这里面一共套了三层

//JSONObject解析请求结果
private void parseJSONWithJSONObject(String jsonData) {

   try {
       JSONObject jsonObject = new JSONObject(jsonData);
       Log.i(TAG, "parseJSONWithJSONObject: code:" + jsonObject.getString("code"));
       Log.i(TAG, "parseJSONWithJSONObject: message:" + jsonObject.getString("message"));


       JSONObject articleList = jsonObject.getJSONObject("articleList");
       Log.i(TAG, "parseJSONWithJSONObject: total:" + articleList.getString("total"));
       Log.i(TAG, "parseJSONWithJSONObject: size:" + articleList.getString("size"));
       Log.i(TAG, "parseJSONWithJSONObject: pages:" + articleList.getString("pages"));
       Log.i(TAG, "parseJSONWithJSONObject: current:" + articleList.getString("current"));
       Log.i(TAG, "parseJSONWithJSONObject: previous:" + articleList.getString("previous"));
       Log.i(TAG, "parseJSONWithJSONObject: next:" + articleList.getString("next"));

       JSONArray records = articleList.getJSONArray("records");
       for (int i = 0; i < records.length(); i++) {
           JSONObject obj = records.getJSONObject(i);
           Log.i(TAG, "parseJSONWithJSONObject: #######################################");
           Log.i(TAG, "parseJSONWithJSONObject: id:" + obj.getString("id"));
           Log.i(TAG, "parseJSONWithJSONObject: title:" + obj.getString("title"));
           Log.i(TAG, "parseJSONWithJSONObject: author:" + obj.getString("author"));
           Log.i(TAG, "parseJSONWithJSONObject: type:" + obj.getString("type"));
           Log.i(TAG, "parseJSONWithJSONObject: status:" + obj.getString("status"));
           Log.i(TAG, "parseJSONWithJSONObject: likeAmount:" + obj.getString("likeAmount"));
           Log.i(TAG, "parseJSONWithJSONObject: collectionAmount:"
                   + obj.getString("collectionAmount"));
       }
   } catch (JSONException e) {
       e.printStackTrace();
   }
}


二、明天计划的事情

完成任务十


三、遇到的问题    

MVP模式,简单的逻辑还能理解,但遇到稍微复杂的界面、功能就感觉无从下手


四、收获

 调用系统电话的两种方式,以前只会跳转到拨号界面

解析嵌套的JSON 




返回列表 返回列表
评论

    分享到