无法使用SMSmanager在Android中将通话日志作为消息发送

huangapple 未分类评论50阅读模式
英文:

Can not send call log as message using SMSmanager Android

问题

我使用了意图(Intent)和待定意图(PendingIntent)来发送消息。当我发送一些文本时,代码运行良好。
问题是,当我通过短信管理器(SmsManager)将通话记录作为字符串发送时,它从不发送。

我可以使用这段代码发送简单的文本消息,但是当我添加复制通话记录的字符串时,它就不会发送消息。

我已经在清单文件中授予了通话记录权限和发送/接收短信的权限。

我的代码如下:-

MainActivity.java

package com.example.sendmessage;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.TimeZone;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    
    public void sendme(View v) {
        Cursor mCursor = managedQuery(CallLog.Calls.CONTENT_URI, null, null, null, null);
        int number = mCursor.getColumnIndex(CallLog.Calls.NUMBER);
        int date = mCursor.getColumnIndex(CallLog.Calls.DATE);
        int duration = mCursor.getColumnIndex(CallLog.Calls.DURATION);
        int type = mCursor.getColumnIndex(CallLog.Calls.TYPE);
        
        StringBuilder sb = new StringBuilder();
       
        while (mCursor.moveToNext()) {
            String phnumber = mCursor.getString(number);
            String callduration = mCursor.getString(duration);
            String calltype = mCursor.getString(type);
            String calldate = mCursor.getString(date);
        	
        	Date mDate = null;
        	
            String givenDateString = calldate;
            SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);
            sdf.setTimeZone(TimeZone.getTimeZone("GMT+0530"));
            
            try {
                mDate = (Date) sdf.parse(givenDateString);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            
            String callTypeStr = "";
        	
            switch (Integer.parseInt(calltype)) {
                case CallLog.Calls.OUTGOING_TYPE:
                    callTypeStr = "OutGoing";
                    break;
                case CallLog.Calls.INCOMING_TYPE:
                    callTypeStr = "Incoming";
                    break;
                case CallLog.Calls.MISSED_TYPE:
                    callTypeStr = "Missed";
                    break;
            }
        	
            sb.append("Phone Number: " + phnumber);
            sb.append("Call Duration: " + callduration);
            sb.append("Call Type: " + callTypeStr);
            sb.append("Date: " + mDate);
            sb.append("------------------------------");
            sb.append(System.getProperty("line.separator"));
        }
    	
        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
        PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
    	
        SmsManager sms = SmsManager.getDefault();
        Toast.makeText(getApplicationContext(), sb.toString(), Toast.LENGTH_LONG).show();
        sms.sendTextMessage("9074560566", null, sb.toString().substring(0, 500), pi, null); 
    }
}

请提供一个解决方案。

英文:

I used intent and pending intent to send message. The code is working fine when I sent some texts.
The problem is that, while sending call logs as String by sms manger it never send it.

I can send simple text messages using this code but when i adding the string which copies call log it do not send the message.

I have given call log permission and send and receive sms permissions in manifest.

My code is here :-

MainActivity.java

package com.example.sendmessage;


import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.TimeZone;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
 import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
public void sendme(View v)
{
	
	
	Cursor mCursor=managedQuery(CallLog.Calls.CONTENT_URI, null, null, null, null);
    int number=mCursor.getColumnIndex(CallLog.Calls.NUMBER);
    int date=mCursor.getColumnIndex(CallLog.Calls.DATE);
    int duration=mCursor.getColumnIndex(CallLog.Calls.DURATION);
    int type=mCursor.getColumnIndex(CallLog.Calls.TYPE);
    
    StringBuilder sb=new StringBuilder();
   
    while (mCursor.moveToNext())
    {
    	String phnumber=mCursor.getString(number);
    	String callduration=mCursor.getString(duration);
    	String calltype=mCursor.getString(type);
    	String calldate=mCursor.getString(date);
    	
    	
    	Date mDate = null;
    	//---------------------------------------------------
    	String givenDateString = calldate;// dateTime variable is the variable in which you are storing the date currently
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);
        sdf.setTimeZone(TimeZone.getTimeZone("GMT+0530"));
        try {
            mDate = (Date) sdf.parse(givenDateString);
          //  timeInMilliseconds = mDate.getTime();

        } catch (ParseException e) {
            e.printStackTrace();
        }
        //-------------------------------------------------
   // 	Date d=new Date(calldate);
    	String callTypeStr="";
    	
    	switch (Integer.parseInt(calltype)) {
    	case CallLog.Calls.OUTGOING_TYPE:
    	 callTypeStr="OutGoing";
    	break;
    	
    	case CallLog.Calls.INCOMING_TYPE:
    		callTypeStr="Incoming";
    		break;
    	case CallLog.Calls.MISSED_TYPE:
    		callTypeStr="Missed";
    		break;
    	}
    
    	
    	sb.append("Phone Number" + phnumber);
    	sb.append("Call duration" + callduration);
    	sb.append("call type" + callTypeStr);
    	sb.append("Date" + mDate);
    	sb.append("------------------------------");
    	sb.append(System.getProperty("line.separator"));
    }
	Intent intent=new Intent(getApplicationContext(),MainActivity.class);  
	PendingIntent pi=PendingIntent.getActivity(getApplicationContext(), 0, intent,0);  
	
	//Get the SmsManager instance and call the sendTextMessage method to send message                 
	SmsManager sms=SmsManager.getDefault();
	Toast.makeText(getApplicationContext(), sb.toString(), Toast.LENGTH_LONG).show();
	sms.sendTextMessage("9074560566", null, sb.toString().substring(0, 500), pi,null); 
	
  }

  }

kindly please give a solution.

huangapple
  • 本文由 发表于 2020年4月8日 15:52:48
  • 转载请务必保留本文链接:https://java.coder-hub.com/61095799.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定