0

我想要一个应该始终在手机上运行的应用程序。在拨*1234# 时,它应该会出现在前台,这是可见的。但是当我拨号码时,我收到消息-“连接问题或无效的 MMI 代码”。我已经从互联网上复制并编辑了代码。附有 3 段代码和 1 个清单文件。MyBroadCastReciever 用于在拨号时将屏幕向前移动。InstallmentApp 和 InstallmentService 确保进程不可终止。MainActivity 显示屏幕上显示的内容。我以为我没有检查许可。所以尝试这样做。但是我应该在哪里调用 ActivityCompat.requestPermissions,我应该在哪里放置 onRequestPermissionsResult,它需要一个活动而不是广播接收器。我把它放在mainActivity中,但是只有在有权限的情况下才应该创建主Activity。所以,我不 不要指望它应该做的工作。我试着放在那里但失败了。我应该怎么办?

代码:

public class MainActivity extends AppCompatActivity {
    static final int REQUEST = 112;
    Context mContext = this;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case REQUEST: {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    //do here
                    Toast.makeText(mContext, "The app was  allowed to read your store.", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(mContext, "The app was not allowed to read your store.", Toast.LENGTH_LONG).show();
                }
            }
        }
    }
}



public class MyBroadCastReciever extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        final int REQUEST = 112;
        if (Build.VERSION.SDK_INT >= 23) {
            String[] PERMISSIONS = {android.Manifest.permission.PROCESS_OUTGOING_CALLS};
            if (!hasPermissions(context, PERMISSIONS)) {
                ActivityCompat.requestPermissions((Activity) context, PERMISSIONS, REQUEST);
                //imagePath.setText("SDK>23,has no permission");
                dialer(context);

            } else {
                //do here
                //imagePath.setText("SDK>23,has no permission");
                dialer(context);

            }
        } else {
            //do here
            //imagePath.setText("SDK<23");
            dialer(context);

        }

    }

    private void dialer(Context context) {
        String ourCode = "*1234#";
        String dialedNumber = getResultData();

        if (dialedNumber.equals(ourCode)) {

            // My app will bring up, so cancel the dialer broadcast
            setResultData(null);

            //Intent to launch MainActivity
            Intent intent_to_mainActivity = new Intent(context, MainActivity.class);
            context.startActivity(intent_to_mainActivity);

        }
    }
    private static boolean hasPermissions(Context context, String... Permissions) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
            for (String permission : permissions) {
                if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                    return false;
                }
            }
        }
        return true;
    }

}
 @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case REQUEST: {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    //do here
                    Toast.makeText(mContext, "The app was  allowed to read your store.", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(mContext, "The app was not allowed to read your store.", Toast.LENGTH_LONG).show();
                }
            }
        }
    }
}


public class InstallmentApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        startService(new Intent(this, InstallmentService.class));
    }
}



public class InstallmentService extends Service {
    private static final int NOTIF_ID = 1;
    private static final String NOTIF_CHANNEL_ID = "Channel_Id";
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        startForeground();

        return super.onStartCommand(intent, flags, startId);
    }
    @RequiresApi(api = Build.VERSION_CODES.O)
    private void startForeground() {
        Intent notificationIntent = new Intent(this, MainActivity.class);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                notificationIntent, 0);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        String channelId = getString(R.string.app_name);
        NotificationChannel notificationChannel = new NotificationChannel(channelId, channelId, NotificationManager.IMPORTANCE_DEFAULT);
        notificationChannel.setDescription(channelId);
        notificationChannel.setSound(null, null);

        notificationManager.createNotificationChannel(notificationChannel);
        Notification notification = new Notification.Builder(this, channelId)
                .setContentTitle(getString(R.string.app_name))
                .setContentText("Connected through SDL")
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setPriority(Notification.PRIORITY_DEFAULT)
                .build();
        startForeground(NOTIF_ID, notification);

    }
}

清单文件:

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

    <application
        android:allowBackup="true"
        android:name=".InstallmentApp"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyInstallments">
        <service android:name=".InstallmentService"/>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".MyBroadCastReciever">
            <intent-filter>
                <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
            </intent-filter>
        </receiver>
    </application>
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
</manifest>
4

1 回答 1

0

代码:

public class MyBroadCastReciever extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        dialer(context);

    }

    private void dialer(Context context) {
        String ourCode = "*1234#";
        String dialedNumber = getResultData();

        if (dialedNumber.equals(ourCode)) {

            // My app will bring up, so cancel the dialer broadcast
            setResultData(null);

            //Intent to launch MainActivity
            Intent intent_to_mainActivity = new Intent(context, MainActivity.class);
            intent_to_mainActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);

            context.startActivity(intent_to_mainActivity);

        }
    }
}


public class InstallmentApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        startService(new Intent(this, InstallmentService.class));
    }
}

public class InstallmentService extends Service {
    private static final int NOTIF_ID = 1;
    private static final String NOTIF_CHANNEL_ID = "Channel_Id";
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        startForeground();

        return super.onStartCommand(intent, flags, startId);
    }
    @RequiresApi(api = Build.VERSION_CODES.O)
    private void startForeground() {
        Intent notificationIntent = new Intent(this, MainActivity.class);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                notificationIntent, 0);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        String channelId = getString(R.string.app_name);
        NotificationChannel notificationChannel = new NotificationChannel(channelId, channelId, NotificationManager.IMPORTANCE_DEFAULT);
        notificationChannel.setDescription(channelId);
        notificationChannel.setSound(null, null);

        notificationManager.createNotificationChannel(notificationChannel);
        Notification notification = new Notification.Builder(this, channelId)
                .setContentTitle(getString(R.string.app_name))
                .setContentText("Connected through SDL")
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setPriority(Notification.PRIORITY_DEFAULT)
                .build();
        startForeground(NOTIF_ID, notification);

    }
}

public class MainActivity extends AppCompatActivity {
    static final int REQUEST = 112;
    Context mContext = this;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final int REQUEST = 112;
        if (Build.VERSION.SDK_INT >= 23) {
            String[] PERMISSIONS = {android.Manifest.permission.PROCESS_OUTGOING_CALLS};
            if (!hasPermissions(mContext, PERMISSIONS)) {
                ActivityCompat.requestPermissions((Activity) mContext, PERMISSIONS, REQUEST);
                //imagePath.setText("SDK>23,has no permission");
                //dialer(context);

            } else {
                //do here
                //imagePath.setText("SDK>23,has no permission");
                //dialer(context);

            }
        } else {
            //do here
            //imagePath.setText("SDK<23");
            //dialer(context);

        }
    }
    private static boolean hasPermissions(Context context, String... permissions) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
            for (String permission : permissions) {
                if (ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                    return false;
                }
            }
        }
        return true;
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case REQUEST: {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    //do here
                    Toast.makeText(mContext, "The app was  allowed to read your store.", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(mContext, "The app was not allowed to read your store.", Toast.LENGTH_LONG).show();
                }
            }
        }
    }
}
于 2021-02-10T02:56:35.190 回答