Click here to Skip to main content
15,884,010 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
When using a broadcast receiver in version 21 it works normally, but if version 28 does not work

What I have tried:

ublic class service_class extends  Service
{
	
MediaPlayer m;
	@Override
	public IBinder onBind(Intent p1)
	{
		// TODO: Implement this method
		return null;
	}

	@Override
	public void onCreate()
	{
		//registerReceiver(c,new IntentFilter("A"));
		m=MediaPlayer.create(this,R.raw.sound);
		//m.start();
		super.onCreate();
		
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId)
	{
		m.start();
		
		// TODO: Implement this method
		return super.onStartCommand(intent, flags, startId);
	
	}

	
	@Override
	public void onDestroy()
	{
		// TODO: Implement this method
		super.onDestroy();
		//m.start();
	}
	
	
}



Broadcast receiver
public class BroadCastRecevier extends BroadcastReceiver
{

	@Override
	public void onReceive(Context p1, Intent p2)
	{
		if(p2.getAction().equals("android.intent.action.USER_PRESENT")){
		
			Intent i=new Intent(p1,service_class.class);
			p1.startService(i);
			Toast.makeText(p1,"service",Toast.LENGTH_LONG).show();
		}
	}
	

}

Android manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mycompany.notificathon" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
		android:resizeableActivity = "true">
        <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=".service_class"/>
		<receiver android:name=".BroadCastRecevier" 
			android:exported="true">
			<intent-filter>
				<action android:name="android.intent.action.USER_PRESENT"/>
			</intent-filter>
		</receiver>
			
    </application>

</manifest>
Posted
Comments
David Crow 16-Sep-20 8:35am    
So what is the problem, exactly?
Mohamed Badaouy 16-Sep-20 13:42pm    
I am using a broadcast receiver when using the action in the Android manifest, the broadcast works if it is version 21 or if version 28 does not broadcast
David Crow 16-Sep-20 14:25pm    
Where is the call to registerReceiver()? I think you need to call it, passing to it the instance of BroadCastRecevier.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900