跳转到帖子

游客您好,欢迎来到黑客世界论坛!您可以在这里进行注册。

赤队小组-代号1949(原CHT攻防小组)在这个瞬息万变的网络时代,我们保持初心,创造最好的社区来共同交流网络技术。您可以在论坛获取黑客攻防技巧与知识,您也可以加入我们的Telegram交流群 共同实时探讨交流。论坛禁止各种广告,请注册用户查看我们的使用与隐私策略,谢谢您的配合。小组成员可以获取论坛隐藏内容!

TheHackerWorld官方

精选回复

发布于

1895087-20220723135904839-2007603919.png

 

 

1895087-20220723141230391-1832897173.png

 

 

 

 

 

 

 

 

 

1895087-20220723140004406-900090289.png

 

 

 

 

 

1895087-20220723141231105-1170091929.png

 

 

 

 

 

 

===========================================================================================

 

 

 

 

 

 

 

布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp">

    <Button
        android:id="@+id/btn_send_shock"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="发送震动广播"
        android:textColor="@color/black"
        android:textSize="17sp" />

</LinearLayout>

1895087-20220723140241351-496795167.png

 

 1895087-20220723140251627-1348199235.png

 

 

 

 

 

 

 

 

 

 

 

 

代码:

package com.example.myapplication;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.receiver.ShockReceiver;


public class MainActivity extends AppCompatActivity implements View.OnClickListener
{

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


        findViewById(R.id.btn_send_shock).setOnClickListener(this);
    }

    @Override
    public void onClick(View v)
    {
        if (v.getId() == R.id.btn_send_shock)
        {
            // Android8.0之后删除了大部分静态注册,防止退出App后仍在接收广播,
            // 为了让应用能够继续接收静态广播,需要给静态注册的广播指定包名。
            String receiverPath = "com.example.myapplication.receiver.ShockReceiver";

            Intent intent = new Intent(ShockReceiver.SHOCK_ACTION); // 创建一个指定动作的意图

            // 发送静态广播之时,需要通过setComponent方法指定接收器的完整路径
            ComponentName componentName  = new ComponentName(this, receiverPath);

            intent.setComponent(componentName); // 设置意图的组件信息

            sendBroadcast(intent); // 发送静态广播

            Toast.makeText(this, "已发送震动广播", Toast.LENGTH_SHORT).show();
        }
    }
}

1895087-20220723140340299-651634970.png

 

 1895087-20220723140350602-937263081.png

 

 

 

 

 

 

 

 

 

ShockReceiver:

 

package com.example.myapplication.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Vibrator;
import android.util.Log;

public class ShockReceiver extends BroadcastReceiver
{
    private static final String TAG = "ShockReceiver";

    // 静态注册时候的action、发送广播时的action、接收广播时的action,三者需要保持一致
    public static final String SHOCK_ACTION = "com.example.myapplication.receiver.ShockReceiver";


    @Override
    public void onReceive(Context context, Intent intent)
    {
        Log.d(TAG, "onReceive");
        if (intent.getAction().equals(ShockReceiver.SHOCK_ACTION))
        {
            // 从系统服务中获取震动管理器
            Vibrator vb = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
            
            vb.vibrate(500); // 命令震动器吱吱个若干秒,这里的500表示500毫秒
        }
    }

}

1895087-20220723140435695-668117200.png

 

 

 

 

 

 

 

mainifest.xml:

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

    <uses-permission android:name="android.permission.VIBRATE" />


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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name=".receiver.ShockReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.example.myapplication.receiver.ShockReceiver" />
            </intent-filter>
        </receiver>

    </application>

</manifest>

 

1895087-20220723140635688-1305692632.png

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

1895087-20220723140537617-746258465.png

 

 1895087-20220723140552583-360782099.png

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

1895087-20220723141053137-721690133.png

 

 1895087-20220723141113168-1865244231.png

 

 1895087-20220723141128041-987671853.png

 

 1895087-20220723141200376-934478517.png

 

 1895087-20220723141215596-1066893333.png

 

创建帐户或登录后发表意见

最近浏览 0

  • 没有会员查看此页面。