【问题标题】:open application After incoming or outgoing call diconnection打开应用程序 来电或去电断开后
【发布时间】:2015-08-23 20:00:46
【问题描述】:

我正在创建一个应用程序,我在 listview 中获取通话记录,现在我正在尝试在任何来电或去电断开后,我想打开那个 listview,但我不知道我的应用程序没有打开。我的 listview 页面在任何调用后都不显示,以下是我的代码

     public class MainActivity extends Activity {

     private ListView listview=null;
     private String callType=null;
     private String phoneNumber=null;
     private String callDate=null;
     private String callDuration=null;
     private Date callDateTime=null;

     private List<CallData> list=new ArrayList<CallData>();
     private Context context=null;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  context=this;

  listview=(ListView)findViewById(R.id.listView_calldata);

   getCallDetails();
   CustomAdapter adapter=new CustomAdapter(MainActivity.this, list);
   listview.setAdapter(adapter);
 }

  public void getCallDetails()
     {

   Cursor managedCursor =  getContentResolver().query(CallLog.Calls.CONTENT_URI,null, null,null, null);



         int number = managedCursor.getColumnIndex( CallLog.Calls.NUMBER );
         int type = managedCursor.getColumnIndex( CallLog.Calls.TYPE );
         int date = managedCursor.getColumnIndex( CallLog.Calls.DATE);
         int duration = managedCursor.getColumnIndex( CallLog.Calls.DURATION);


         while (managedCursor.moveToNext())
         {

             phoneNumber = managedCursor.getString(number);
             callType = managedCursor.getString(type);
             callDate = managedCursor.getString(date);

             callDateTime = new Date(Long.valueOf(callDate));

             callDuration = managedCursor.getString(duration);

             String cType = null;

             int cTypeCode = Integer.parseInt(callType);

             switch(cTypeCode)
                 {
                         case CallLog.Calls.OUTGOING_TYPE:
                         cType = "OUTGOING";
                         break;

                         case CallLog.Calls.INCOMING_TYPE:
                         cType= "INCOMING";
                         break;

                         case CallLog.Calls.MISSED_TYPE:
                         cType = "MISSED";
                         break;
                 }

             CallData calldata=new CallData(cType, phoneNumber, callDateTime, callDuration);
             list.add(calldata);
         }

         managedCursor.close();
     }
}

电话呼叫接收器

public abstract class PhonecallReceiver extends BroadcastReceiver {

    //The receiver will be recreated whenever android feels like it.  We need a static variable to remember data between instantiations
    static PhonecallStartEndDetector listener;
    String outgoingSavedNumber;
    protected Context savedContext;


    @Override
    public void onReceive(Context context, Intent intent) {
        savedContext = context;
        if(listener == null){
            listener = new PhonecallStartEndDetector();
        }

        //We listen to two intents.  The new outgoing call only tells us of an outgoing call.  We use it to get the number.
        if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
            listener.setOutgoingNumber(intent.getExtras().getString("android.intent.extra.PHONE_NUMBER"));
            return;
        }

        //The other intent tells us the phone state changed.  Here we set a listener to deal with it
        TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); 
        telephony.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
    }

    //Derived classes should override these to respond to specific events of interest
    protected abstract void onIncomingCallStarted(String number, Date start);
    protected abstract void onOutgoingCallStarted(String number, Date start);
    protected abstract void onIncomingCallEnded(String number, Date start, Date end); 
    protected abstract void onOutgoingCallEnded(String number, Date start, Date end);
    protected abstract void onMissedCall(String number, Date start);

    //Deals with actual events
    public class PhonecallStartEndDetector extends PhoneStateListener {
        int lastState = TelephonyManager.CALL_STATE_IDLE;
        Date callStartTime;
        boolean isIncoming;
        String savedNumber;  //because the passed incoming is only valid in ringing

        public PhonecallStartEndDetector() {}

        //The outgoing number is only sent via a separate intent, so we need to store it out of band
        public void setOutgoingNumber(String number){
            savedNumber = number;
        }

        //Incoming call-  goes from IDLE to RINGING when it rings, to OFFHOOK when it's answered, to IDLE when its hung up
        //Outgoing call-  goes from IDLE to OFFHOOK when it dials out, to IDLE when hung up
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            super.onCallStateChanged(state, incomingNumber);
            if(lastState == state){
                //No change, debounce extras
                return;
            }
            switch (state) {
                case TelephonyManager.CALL_STATE_RINGING:
                    isIncoming = true;
                    callStartTime = new Date();
                    savedNumber = incomingNumber;
                    onIncomingCallStarted(incomingNumber, callStartTime);
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    //Transition of ringing->offhook are pickups of incoming calls.  Nothing donw on them
                    if(lastState != TelephonyManager.CALL_STATE_RINGING){
                        isIncoming = false;
                        callStartTime = new Date();
                        onOutgoingCallStarted(savedNumber, callStartTime);                      
                    }
                    break;
                case TelephonyManager.CALL_STATE_IDLE:
                    //Went to idle-  this is the end of a call.  What type depends on previous state(s)
                    if(lastState == TelephonyManager.CALL_STATE_RINGING){
                        //Ring but no pickup-  a miss
                        onMissedCall(savedNumber, callStartTime);
                    }
                    else if(isIncoming){
                        onIncomingCallEnded(savedNumber, callStartTime, new Date());                        
                    }
                    else{
                        onOutgoingCallEnded(savedNumber, callStartTime, new Date());                                                
                    }
                    break;
            }
            lastState = state;
        }

    }

清单

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.tests"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.READ_CONTACTS"/>

    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <receiver android:name=".PhonecallReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
        </intent-filter>
    </receiver>

        <!--This part is inside the application-->

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >

             <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>
    </application>

</manifest>

【问题讨论】:

  • 发布您的清单并展示您如何注册BroadcastReceiver
  • @DavidWasser 查看我的更新问题
  • 我相信this 是您正在寻找的。这会从另一个应用程序打开一个应用程序,在调用结束时调用它。
  • 您在哪里注册BroadcastReceiver?我没有看到任何这样做的代码。
  • 检查现在编辑的清单

标签: android android-intent android-listview broadcastreceiver


【解决方案1】:

您已将您的PhonecallReceiver 声明为abstract。这意味着 Android 无法在需要时实例化它。你需要重新考虑你的架构。清单中声明的​​BroadcastReceivers 不能是abstract,因为Android 需要能够根据需要自行实例化它们。要么使您的 PhonecallReceiver 具体(非抽象),要么在清单中将具体子类添加为 &lt;receiver&gt;

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-08
  • 2018-01-31
  • 2019-01-01
相关资源
最近更新 更多