2012-08-13 5 views
2

내가 원한다. callrecieve 활동을 통해 들어오는 호출을 감지하는 broadcastreceiver 클래스를 만들었습니다. 이제 전화가 올 자마자 나는 현재 시간을 찾아 다른 활동을 통해 보여주고 싶다. 나는 이것을 위해 의도를 사용했다. 하지만 여전히 나는 전화를받을 때 표시되는 시간 값을 필요에 따라 얻지 못하고 있습니다. 내가 사용하고있는 모든 코드를 게시했습니다. 도움은 지금은 활동에 START_TIME의 가치를 보내려면 여기 브로드 캐스트 리시버에서 안드로이드로 데이터를 보내는 방법

수신 전화의 전화의 상태를 확인하고 의도

package com.mohit; 

import android.app.Activity; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.telephony.TelephonyManager; 
import android.util.Log; 


public class callreceive extends Activity { 
public class receive extends BroadcastReceiver { 


    long start_time,end_time,dif_sec; 
    int flag=0; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
    Bundle extras = intent.getExtras(); 
    if (extras != null) { 
     String state = extras.getString(TelephonyManager.EXTRA_STATE); 

     Log.w("DEBUG", state); 
     if ((state.equals(TelephonyManager.EXTRA_STATE_IDLE))&&flag==0) 
     { 
     flag=1; 
     start_time=System.currentTimeMillis(); 
     Intent intent1 = new Intent();//(callreceive.this,TestActivity.class); 
     intent1.setAction(Intent.ACTION_SEND); 
     intent1.putExtra("start_time",start_time); 
     startActivity(intent1); 

     } 
    } 
    } 
} 
}  

방송을 담당하는 방송 수신 활동 좋을 것 loop가 실행 되 자마자. 다음은 수신 액티비티 - 테스트 액티비티에 대한 코드입니다. 여기

package com.mohit; 

import com.mohit.R; 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.widget.TextView; 

public class TestActivity extends Activity { 

    TextView output1,output2; 
    long start_time,end_time; 
    int flag=0; 
    long dif_sec=0; 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 
      output1=(TextView)findViewById(R.id.textView1); 


      Intent extras = getIntent(); 
       if (extras!=null) 
       { 

        if (flag==0) 
        { 
         flag=1; 
       dif_sec=extras.getLongExtra("start_time",0); 
        } 
       } 
       output1.setText(String.valueOf(dif_sec)); 

       } 

} 

내가 테스트 acitvity을 통해 START_TIME의 값을 인쇄 할 수 없습니다 오전 안드로이드 매니페스트 파일

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.mohit" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk android:minSdkVersion="7" /> 
<uses-permission android:name="android.permission.READ_CONTACTS" /> 
<uses-permission android:name="android.permission.READ_PHONE_STATE"/> 

    <application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" > 
    <activity android:name="TestActivity" > 
     <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
     <category android:name="android.intent.category.LAUNCHER" /> 

      </intent-filter> 
    </activity> 


    <activity android:name="callreceive" > 
    <receiver android:name="receive" > 
     <intent-filter> 
    <action android:name="android.intent.action.SEND" /> 
    <action android:name="android.intent.action.PHONE_STATE"></action> 
    <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </receiver> 
    </activity> 

</application> 

</manifest> 

입니다. 나는 무엇인가 놓치고 있거나, 무엇인가 틀린가? Plz 도움말 고맙습니다

답변

6

NewActivity라는 새로운 활동을 만듭니다. 이 작업을 수행합니다

public class ILoveUrielFrankelActivity extends Activity { 
    private TextView mTextView; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     Bundle extras = getIntent().getExtras(); 
     long displayTime = extras.getLong("start_time"); 
     TextView mTextView = (TextView)findViewById(R.id.uriel); 
     mTextView.setText(displayTime); 
    } 

내부 고해상도/레이아웃/main.xml에

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
    <TextView android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/uriel"/> 

</LinearLayout> 
+0

감사 우리엘 수도 PLZ주고 :

public class callreceiver extends BroadcastReceiver { long start_time; int flag=0; @Override public void onReceive(Context context, Intent intent) { Bundle extras = intent.getExtras(); if (extras != null) { String state = extras.getString(TelephonyManager.EXTRA_STATE); Log.w("DEBUG", state); if ((state.equals(TelephonyManager.EXTRA_STATE_RINGING))&&flag==0) { flag=1; start_time=System.currentTimeMillis(); Intent intent = new Intent(context,NewActivity.class); intent.putExtra("start_time",start_time) startActivity(intent); } } 

을 이제 엑스트라를 얻을 새로운 클래스를 생성 나 동적으로이 의도를 받기위한 코드 ... 나는 활동을 통해 start_time의 값을 인쇄하고 싶다 ... – aps109

+0

Ok. 내 대답을 편집 할 것입니다 ... –

+0

보다 uriel .. 정말 당신의 도움을 주셔서 감사합니다 – aps109

관련 문제