1

我有两个活动MainActivityRecordHistory

我在MainActivity.
我也能够获得广播意图,并且OnRecieve
我正在启动RecordHistoryActivity。
但我的问题是在可见MainActivity时也会启动。 但我只想发起活动。 RecordHistory
RecordHistory

REcordHistory.java

Bundle bundle = getIntent().getExtras();
    String phonenumbString = bundle.getString("phonenumber");
    Toast.makeText(this, phonenumbString, Toast.LENGTH_SHORT).show();

    final Dialog dialog = new Dialog(RecordHistory.this);
    Window window = dialog.getWindow();
    WindowManager.LayoutParams wlp = window.getAttributes();

    wlp.gravity = Gravity.CENTER;
    wlp.x=0;
    wlp.y=0;
    //
    wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
    window.setAttributes(wlp);

    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.show);
    dialog.setCanceledOnTouchOutside(true);
    dialog.setCancelable(true);
    dialog.show();
}

MainActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);     

    broadcastIntent();
}

public void broadcastIntent() {
    Intent intent = new Intent();
    //Toast.makeText(MainActivity.this,"Running ",Toast.LENGTH_LONG).show();
    intent.setAction("com.example.downloadfileservice.IncomingCallListener");
    sendBroadcast(intent);
}

public void onClick(View view) {

}
}   

IncomingCallReceiver.java

public class IncomingCallListener extends BroadcastReceiver {
private static long timeStarted = -1L; // IMPORTANT!
private static long timeAnswered;
private static long timeEnded;
private static boolean isRoaming;
private static String callerNumber;
@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Log.d("TEST","OnRecieve ");
Bundle extras = intent.getExtras();
if (extras == null) return;
String state = extras.getString(TelephonyManager.EXTRA_STATE);
if (state == null) return;

// phone is ringing
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {           

    timeStarted = System.currentTimeMillis();
    String timStarted = new SimpleDateFormat("MMM dd,yyyy HH:mm:ss").format(Calendar.getInstance().getTime());
    callerNumber = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
    TelephonyManager tm = (TelephonyManager) context.getSystemService(
            Context.TELEPHONY_SERVICE);
    isRoaming = tm.isNetworkRoaming();

    // set timeAnswered to -1;
    timeAnswered = -1L;
    SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm");

    Date resultdate = new Date(timeAnswered);
    Log.d("TEST","TimePRatik "+sdf.format(resultdate));
    Log.d("TEST", "timeStarted: " + (timStarted));
    Log.d("TEST", "caller number: " + callerNumber);
    Log.d("TEST", "isRoaming: " + isRoaming);
    return;
}

// call was answered
if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK) && timeStarted != -1L) {
    String timAnswered = new SimpleDateFormat("MMM dd,yyyy HH:mm:ss").format(Calendar.getInstance().getTime());
    timeAnswered = System.currentTimeMillis();
    Log.d("TEST", "timeAnswered: " + timAnswered);

    return;
}

// call was ended
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE) && timeStarted != -1L) {
    String timEnded = new SimpleDateFormat("MMM dd,yyyy HH:mm:ss").format(Calendar.getInstance().getTime());
    timeEnded = System.currentTimeMillis();
    Log.d("TEST", "timeEnded: " + timEnded);
    timeStarted = -1L; // DON'T FORGET!
    Intent i = new Intent(context,RecordHistory.class);
    i.putExtra("phonenumber", callerNumber);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    Log.d("TEST", "Context of "+context.getClass());
    context.setTheme(android.R.style.Theme_Translucent_NoTitleBar);
    context.startActivity(i); 

    return;
}
}

}

4

0 回答 0