gpt4 book ai didi

android - 为什么 BroadcastReceiver 不起作用?

转载 作者:行者123 更新时间:2023-11-29 01:19:58 25 4
gpt4 key购买 nike

我有一项 Activity 和一项服务。在服务中,我有一个 BroadcastReceiver,我想在其中输出一条消息。当我启动上述服务时,为什么它不显示消息?

Activity :

public class MainActivity extends AppCompatActivity
{
private Button button_sel; //adaugat

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

checkLicenta(); //adaugat

// adaugat
findViewById(R.id.button).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent(MainActivity.this, MainService.class);
startService(intent);
Toast.makeText(MainActivity.this, "Service, Running", Toast.LENGTH_SHORT).show();
}
}); //pana aici

// adaugat
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent(MainActivity.this, MainService.class);
stopService(intent);
Toast.makeText(MainActivity.this, "Service, Stopped", Toast.LENGTH_SHORT).show();
}
}); //pana aici
}

}

服务:

public class MainService extends Service
{
private static final String TAG = "HelloService";

private boolean isRunning = false;

@Override
public void onCreate()
{
registerReceiver(counter, new IntentFilter(Intent.ACTION_SCREEN_ON));
Log.i(TAG, "Service, onCreate");
Toast.makeText(MainService.this, "Service, Created", Toast.LENGTH_SHORT).show();
isRunning = true;
}

private BroadcastReceiver counter = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(MainService.this, "LALALA", Toast.LENGTH_SHORT).show();
}
};

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.i(TAG, "Service, onStartCommand");
Toast.makeText(MainService.this, "Service, Started", Toast.LENGTH_SHORT).show();
return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy()
{
Log.i(TAG, "Service, onDestroy");
Toast.makeText(MainService.this, "Service, Destroyed", Toast.LENGTH_SHORT).show();
isRunning = false;
}

@Override
public IBinder onBind(Intent intent)
{
Log.i(TAG, "Service, onBind");
return null;
}
}

更新:由于 ACTION_SCREEN_ON,该消息实际上会在您打开屏幕时出现。

最佳答案

您的代码工作正常,您是否在 AndroidManifest 中声明了您的服务?

   <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name="com.example.test.MainService"
android:exported="true" >
<intent-filter>
<action android:name="com.example.test.MainService" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</service>
</application>

关于android - 为什么 BroadcastReceiver 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37459656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com