我想在安卓工作室的新电子邮件上显示简单的吐司......我正在使用接收器,但没有被触发...
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PROVIDER_CHANGED"/>
<data android:scheme="content"/>
</intent-filter>
</receiver>
在接收器中
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Mail received ", Toast.LENGTH_SHORT).show();
Log.i("mail","mail received");
context.startActivity(new Intent(context,BottemNavigationActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
}
我希望这就是你要找的。您也可以在service
中注册,而不是在manifest
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals("com.example.app.START")) {
Toast.makeText(context, "your message", Toast.LENGTH_LONG).show();
}
}
}
由于接收电子邮件不是操作系统的一部分,因此您需要为特定应用程序注册触发器,例如 Gmail
.为此,您需要在清单中编写以下内容:
<receiver android:name="GmailReceiver">
<intent-filter>
<action android:name="android.intent.action.PROVIDER_CHANGED"
android:priority="-10">
</action>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="content" android:host="gmail-ls"
android:pathPattern="/unread/.*">
</data>
</intent-filter>
</receiver>
然后要接收这些电子邮件:
public class GmailReceiver extends BroadcastReceiver{
Context context;
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Email Received!!", Toast.LENGTH_LONG).show();
}
}