【问题标题】:how to send data from broadcast receiver to activity in android如何将数据从广播接收器发送到android中的活动
【发布时间】:2012-08-13 19:04:10
【问题描述】:

这就是我想要做的。我创建了一个广播接收器类,它通过 callrecieve 活动检测来电。现在,一旦有电话,我想找到它的当前时间并通过另一个活动显示它。我已经为此使用了意图。但是我仍然没有得到所需的时间值,当我接到电话时会显示出来。我已经发布了我正在使用的所有代码。任何帮助都会很棒

这是广播接收器活动,它检查电话的状态是否有来电并负责广播意图

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);

        }
    }
  }
}
}    

现在我想在执行循环后立即将 start_time 的值发送到活动。下面是接收activity-Test Activity的代码。

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));

              }

} 

这里是安卓清单文件

<?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>

我无法通过测试活动打印 start_time 的值。我错过了什么还是有什么问题?请帮助 谢谢

【问题讨论】:

    标签: android broadcastreceiver


    【解决方案1】:

    创建一个名为NewActivity的新Activity。并这样做:

    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);
         }
         }
    

    现在,创建一个获得附加功能的新类:

    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);
      }
    

    在 res/layout/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>
    

    【讨论】:

    • 谢谢 uriel 还请给我动态接收此意图的代码...我想通过活动打印 start_time 的值...
    • thans uriel.. 非常感谢您的帮助
    • 嘿..这不起作用... start_time 不显示在安卓屏幕上
    • 我不是懒惰..我已经完成了你所说的一切......但是当有人打电话给我的手机时,它的时间戳仍然不显示......我已经有了xml文件...... . 只是当我接到电话时没有显示时间戳的值......
    • 我使用的是安卓 2.1。有问题吗??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多