我当前正在修改现有的应用代码。我想在mShakeImage.start()发生时调用此类文件的函数。 如何在CallScreen中将函数调用到SingleEventView?
CallScreen
*/
package com.st.BlueMS.demos.AccEvent;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
public class CallScreen extends AppCompatActivity{
protected void CallingFunction() {
final int REQUEST_PHONE_CALL = 1;
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel: 991"));
if (ContextCompat.checkSelfPermission(CallScreen.this,
Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(CallScreen.this, new String[]{Manifest.permission.CALL_PHONE}, REQUEST_PHONE_CALL);
} else {
startActivity(callIntent);
}
}
}
SingleEventView
public class SingleEventView extends LinearLayout implements EventView {
public SingleEventView(Context context) {
super(context);
init(context);
}
public SingleEventView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public SingleEventView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public SingleEventView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context);
}
private @DrawableRes int mCurrentIconId;
private ImageView mEventIcon;
private TextView mEventText;
private RepeatAnimator mShakeImage;
private FeatureAccelerationEvent.DetectableEvent mCurrentDetectEvent = FeatureAccelerationEvent.DetectableEvent.NONE;
/**
* string were write the number of steps
*/
private String mStepCountTextFormat;
private void changeIcon(@DrawableRes int icon){
if(icon==mCurrentIconId)
return;
mEventIcon.setImageResource(icon);
mCurrentIconId=icon;
}
private void init(Context context){
inflate(context, R.layout.view_acc_event_single,this);
mEventIcon = findViewById(R.id.accEvent_singleEventIcon);
mEventText = findViewById(R.id.accEvent_singleEventLabel);
AnimatorSet shakeImage = (AnimatorSet) AnimatorInflater.loadAnimator(context,
R.animator.shake);
mShakeImage = new RepeatAnimator(shakeImage, 5);
mShakeImage.setTarget(mEventIcon);
mStepCountTextFormat = getResources().getString(R.string.stepCounterStringFormat);
}
@Override
public void enableEvent(Node.Type type, FeatureAccelerationEvent.DetectableEvent eventType){
mCurrentDetectEvent = eventType;
changeIcon( EventIconUtil.getDefaultIcon(eventType));
mEventText.setText(null);
}
private void changeOrientationIcon(@AccelerationEvent int event) {
@DrawableRes int newIcon = EventIconUtil.getEventIcon(event);
if (newIcon != mCurrentIconId) {
changeIcon(newIcon);
} else {
mShakeImage.start();
Context context = getContext();
new AlertDialog.Builder(context)
.setTitle("Alert")
.setMessage("This is an alert")
.setNegativeButton("OK", null)
.show();
}
}
@Override
public void displayEvent(@AccelerationEvent int event, int data) {
if(mCurrentDetectEvent == FeatureAccelerationEvent.DetectableEvent.ORIENTATION &&
FeatureAccelerationEvent.hasOrientationEvent(event)) {
changeOrientationIcon(event);
}else {
mShakeImage.start();
Context context = getContext();
new AlertDialog.Builder(context)
.setTitle("Alert")
.setMessage("This is an alert")
.setNegativeButton("OK", null)
.show();
Intent intent = new Intent(getApplicationContext(), CallScreen.class);
}
if(mCurrentDetectEvent ==
FeatureAccelerationEvent.DetectableEvent.PEDOMETER &&``
event==FeatureAccelerationEvent.PEDOMETER && data>=0){
mEventText.setText(String.format(mStepCountTextFormat,data));
}
}
/**
* store the current status of the view, if is transparent or not
* @return object where we store the internal state
*/
@Override
public Parcelable onSaveInstanceState() {
//begin boilerplate code that allows parent classes to save state
Parcelable superState = super.onSaveInstanceState();
return new SingleEventView.SavedState(superState,mCurrentIconId);
}//onSaveInstanceState
/**
* restore the previous state of the view -> if it is transparent or not
* @param state object where we have stored the data
*/
@Override
public void onRestoreInstanceState(Parcelable state) {
//begin boilerplate code so parent classes can restore state
if(!(state instanceof SingleEventView.SavedState)) {
super.onRestoreInstanceState(state);
return;
}
SingleEventView.SavedState ss = (SingleEventView.SavedState)state;
super.onRestoreInstanceState(ss.getSuperState());
//end
changeIcon(ss.getIcon());
}//onRestoreInstanceState
/**
* class used for store the internal state
* see http://stackoverflow.com/questions/3542333/how-to-prevent-custom-views-from-losing-state-across-screen-orientation-changes
*
*/
public class SavedState extends BaseSavedState {
private @DrawableRes int mIconId;
SavedState(Parcelable superState,@DrawableRes int iconId) {
super(superState);
mIconId=iconId;
}
private SavedState(Parcel in) {
super(in);
this.mIconId = in.readInt();
}
@DrawableRes int getIcon(){
return mIconId;
}
@Override
public void writeToParcel(Parcel out, int flags) {
super.writeToParcel(out, flags);
out.writeInt(mIconId);
}
//required field that makes Parcelables from a Parcel
public final Parcelable.Creator<SingleEventView.SavedState> CREATOR =
new Parcelable.Creator<SingleEventView.SavedState>() {
public SingleEventView.SavedState createFromParcel(Parcel in) {
return new SingleEventView.SavedState(in);
}
public SingleEventView.SavedState[] newArray(int size) {
return new SingleEventView.SavedState[size];
}
};
}//SaveState
}
添加通话功能的重要部分
@Override
public void displayEvent(@AccelerationEvent int event, int data) {
if(mCurrentDetectEvent == FeatureAccelerationEvent.DetectableEvent.ORIENTATION &&
FeatureAccelerationEvent.hasOrientationEvent(event)) {
changeOrientationIcon(event);
}else {
mShakeImage.start();
Context context = getContext();
new AlertDialog.Builder(context)
.setTitle("Alert")
.setMessage("This is an alert")
.setNegativeButton("OK", null)
.show();
Intent intent = new Intent(getApplicationContext(), CallScreen.class);
编辑
我能够从调用屏幕调用函数到singleeventview。 但是现在调用函数一旦执行,就不会返回到单事件视图。 我该如何实现?
最新致电代码
公共类CallScreen扩展了AppCompatActivity {
@SuppressLint(“ MissingPermission”)
@Override
受保护的void onStart(){
super.onStart();
final int REQUEST_PHONE_CALL = 1;
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel: 991"));
//----------------------check for permission before placing the call-----
if (ContextCompat.checkSelfPermission(CallScreen.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(CallScreen.this, new String[]{Manifest.permission.CALL_PHONE}, REQUEST_PHONE_CALL);
} else {
//------------places the call-----------------------------------------------
startActivity(callIntent);
}
}
}
最新单人观看代码
@Override
public void displayEvent(@AccelerationEvent int event, int data) {
if(mCurrentDetectEvent ==FeatureAccelerationEvent.DetectableEvent.FREE_FALL)
{
mShakeImage.start();
Context context = getContext();
//-----------test for position of shake image--------------------
new AlertDialog.Builder(context)
.setTitle("Alert")
.setMessage("This is an alert")
.show();
//---------------call function from CallScreen()-----------------
Intent intent = new Intent(context.getApplicationContext(), CallScreen.class);
context.startActivity(intent);
}
答案 0 :(得分:0)
您要通过其他活动并单击警报对话框的“确定”按钮来调用调用函数。我说的对吗?
如果我是对的,则需要在“确定”按钮上执行操作,然后在警报对话框中单击。 您只需要创建一个Object of Calling类,以便可以访问该类的方法。
@Override
public void displayEvent(@AccelerationEvent int event, int data) {
if(mCurrentDetectEvent == FeatureAccelerationEvent.DetectableEvent.ORIENTATION &&
FeatureAccelerationEvent.hasOrientationEvent(event)) {
changeOrientationIcon(event);
}else {
mShakeImage.start();
Context context = getContext();
new AlertDialog.Builder(context)
.setTitle("Alert")
.setMessage("This is an alert")
.setNegativeButton("OK", new View.OnClickListener() {
public void onClick(ViewDialog dialog, int id) {
//do things
//Here you can create the Objet of your Calling class
CallScreen callScreen = new CallScreen();
callScreen.CallingFunction();
}
});
.show();
Intent intent = new Intent(getApplicationContext(), CallScreen.class);
答案 1 :(得分:0)
要启动Activity
,您需要Intent
和Context
。
mShakeImage.start();
Context context = getContext();
new AlertDialog.Builder(context)
.setTitle("Alert")
.setMessage("This is an alert")
.setNegativeButton("OK", null)
.show();
Intent intent = new Intent(getApplicationContext(), CallScreen.class);
context.startActivity(intent);
注意:
启动一项新活动。您将不会收到有关活动何时退出的任何信息。
请注意,如果从Activity外部调用此方法 上下文,那么Intent必须包括 Intent#FLAG_ACTIVITY_NEW_TASK启动标志。这是因为,如果没有 从现有活动开始,则在 放置新活动的位置,因此需要将其放置在 自己的任务。
如果没有活动,此方法将引发ActivityNotFoundException 发现可以运行给定的Intent。